有没有一种方法可以在Mac上生成标准128位GUID(UUID)?
是否有相当于 .NET 的内置函数
Guid.NewGuid();
Cocoa 中
? 我的愿望是生成一个类似于 550e8400-e29b-41d4-a716-446655440000 的字符串,它表示唯一标识符。
Is there a built in function equivalent to .NET's
Guid.NewGuid();
in Cocoa?
My desire is to produce a string along the lines of 550e8400-e29b-41d4-a716-446655440000
which represents a unique identifier.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
至少在 MacOSX 10.5.x 上您可以使用命令行工具“uuidgen”来获取字符串
例如
$ uuidgen
054209C4-3873-4679-8104-3C18AE780512
该命令还有一个选项 -hdr ,可以方便地以标头样式生成它。
请参阅手册页以获取更多信息。
At least on MacOSX 10.5.x you might use the command line tool "uuidgen" to get your string
e.g.
$ uuidgen
054209C4-3873-4679-8104-3C18AE780512
there's also an option -hdr with this comand that conveniently generates it in header style
See man-page for further infos.
UUID 在 Core Foundation 中由 CFUUID 库处理。 您正在寻找的函数是 CFUUID创建。
供进一步搜索参考:这些最常被称为 UUID,术语 GUID 在 Microsoft 世界之外并不经常使用。 您可能会更幸运地使用该搜索词。
UUIDs are handled in Core Foundation, by the CFUUID library. The function you are looking for is CFUUIDCreate.
FYI for further searches: these are most commonly known as UUIDs, the term GUID isn't used very often outside of the Microsoft world. You might have more luck with that search term.
一些代码:
对于字符串 UUID,以下类方法应该可以解决问题:
如果您确实想要字节(而不是字符串):
其中 CFUUIDBytes 是 UUID 字节的结构。
Some code:
For a string UUID, the following class method should do the trick:
if you really want the bytes (not the string):
where CFUUIDBytes is a struct of the UUID bytes.
或者有
uuidgen
命令行工具。or there's the
uuidgen
command line tool.从 10.8 开始你还可以使用:
Since 10.8 you could also use:
您可以在
~/.bash_profile
中创建别名并从终端调用uuid
:You can create an alias in the
~/.bash_profile
and calluuid
from the terminal:从 10.3 左右开始,您可以使用
[[NSProcessInfo processInfo]globallyUniqueString]
。 然而,虽然这个当前生成一个UUID,但它从来没有而且仍然不能保证这样做,所以如果您确实需要一个UUID而不仅仅是任何唯一的字符串,您应该使用 CFUUID。Since 10.3 or so, you can use
[[NSProcessInfo processInfo] globallyUniqueString]
. However, while this currently generates a UUID, it never has been and still isn't guaranteed to do that, so if you really need a UUID and not just any unique string, you should use CFUUID.