有没有一种方法可以在Mac上生成标准128位GUID(UUID)?

发布于 2024-07-09 03:33:37 字数 158 浏览 6 评论 0原文

是否有相当于 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

一影成城 2024-07-16 03:33:37

至少在 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.

白日梦 2024-07-16 03:33:37

一些代码:

对于字符串 UUID,以下类方法应该可以解决问题:

+(NSString*)UUIDString {
    CFUUIDRef theUUID = CFUUIDCreate(NULL);
    CFStringRef string = CFUUIDCreateString(NULL, theUUID);
    CFRelease(theUUID);
    return [(NSString *)string autorelease];
}

如果您确实想要字节(而不是字符串):

+(CFUUIDBytes)UUIDBytes {
    CFUUIDRef theUUID = CFUUIDCreate(NULL);
    CFUUIDBytes bytes = CFUUIDGetUUIDBytes(theUUID);
    CFRelease(theUUID);
    return bytes;
}

其中 CFUUIDBytes 是 UUID 字节的结构

Some code:

For a string UUID, the following class method should do the trick:

+(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 struct of the UUID bytes.

阳光下的泡沫是彩色的 2024-07-16 03:33:37

或者有 uuidgen 命令行工具。

or there's the uuidgen command line tool.

情话墙 2024-07-16 03:33:37

从 10.8 开始你还可以使用:

NSString *uuidString = [[NSUUID UUID] UUIDString];

Since 10.8 you could also use:

NSString *uuidString = [[NSUUID UUID] UUIDString];
你在我安 2024-07-16 03:33:37

您可以在 ~/.bash_profile 中创建别名并从终端调用 uuid

alias uuid="uuidgen | tr '[:upper:]' '[:lower:]'"

You can create an alias in the ~/.bash_profile and call uuid from the terminal:

alias uuid="uuidgen | tr '[:upper:]' '[:lower:]'"
半﹌身腐败 2024-07-16 03:33:37

从 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文