macos 有没有办法在 Mac 上生成标准的 128 位 GUID (UUID)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/291868/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a method to generate a standard 128bit GUID (UUID) on the Mac?
提问by Lawrence Johnston
Is there a built in function equivalent to .NET's
是否有与 .NET 等效的内置函数
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.
我的愿望是生成一个550e8400-e29b-41d4-a716-446655440000
代表唯一标识符的字符串。
回答by J?rg W Mittag
UUIDs are handled in Core Foundation, by the CFUUID library. The function you are looking for is CFUUIDCreate.
UUID 在 Core Foundation 中由 CFUUID 库处理。您正在寻找的功能是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,术语 GUID 在 Microsoft 世界之外并不经常使用。使用该搜索词可能会更幸运。
回答by Barry Wark
Some code:
一些代码:
For a string UUID, the following class method should do the trick:
对于字符串 UUID,以下类方法应该可以解决问题:
+(NSString*)UUIDString {
CFUUIDRef theUUID = CFUUIDCreate(NULL);
CFStringRef string = CFUUIDCreateString(NULL, theUUID);
CFRelease(theUUID);
return [(NSString *)string autorelease];
}
if you really want the bytes (not the string):
如果你真的想要字节(不是字符串):
+(CFUUIDBytes)UUIDBytes {
CFUUIDRef theUUID = CFUUIDCreate(NULL);
CFUUIDBytes bytes = CFUUIDGetUUIDBytes(theUUID);
CFRelease(theUUID);
return bytes;
}
where CFUUIDBytes is a structof the UUID bytes.
其中 CFUUIDBytes 是UUID 字节的结构。
回答by Barry Wark
At least on MacOSX 10.5.x you might use the command line tool "uuidgen" to get your string e.g.
至少在 MacOSX 10.5.x 上,您可以使用命令行工具“uuidgen”来获取字符串,例如
$ uuidgen
$uuidgen
054209C4-3873-4679-8104-3C18AE780512
054209C4-3873-4679-8104-3C18AE780512
there's also an option -hdr with this comand that conveniently generates it in header style
这个命令还有一个选项 -hdr 可以方便地以标题样式生成它
See man-page for further infos.
有关更多信息,请参见手册页。
回答by Lawrence Johnston
or there's the uuidgen
command line tool.
或者有uuidgen
命令行工具。
回答by David Elliman
Since 10.8 you could also use: NSString *uuidString = [[NSUUID UUID] UUIDString];
从 10.8 开始,您还可以使用: NSString *uuidString = [[NSUUID UUID] UUIDString];
回答by waynecolvin
Check out the Wikipediaarticle and the Core Foundationpage.
回答by Peter Hosey
Since 10.3 or so, you can use [[NSProcessInfo processInfo] globallyUniqueString]
. However, while this currentlygenerates a UUID, it never has been and still isn't guaranteed to do that, so if you really need a UUIDand not just any unique string, you should use CFUUID.
从 10.3 左右开始,您可以使用[[NSProcessInfo processInfo] globallyUniqueString]
. 然而,虽然这当前生成了一个 UUID,但它从来没有而且仍然不能保证这样做,所以如果你真的需要一个UUID而不仅仅是任何唯一的字符串,你应该使用 CFUUID。