生成启用 ARC 的 UUID 字符串

发布于 2024-12-23 16:11:07 字数 275 浏览 7 评论 0原文

我需要在启用 ARC 的某些代码中生成 UUID 字符串。

经过一些研究后,我得出的结论是:

CFUUIDRef uuid = CFUUIDCreate(NULL);
NSString *uuidStr = (__bridge_transfer NSString *)CFUUIDCreateString(NULL, uuid);
CFRelease(uuid);

我是否正确使用 __bridge_transfer 来避免泄漏 ARC 下的任何对象?

I need to generate a UUID string in some code with ARC enabled.

After doing some research, this is what I came up with:

CFUUIDRef uuid = CFUUIDCreate(NULL);
NSString *uuidStr = (__bridge_transfer NSString *)CFUUIDCreateString(NULL, uuid);
CFRelease(uuid);

Am I correctly using __bridge_transfer to avoid leaking any objects under ARC?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

小草泠泠 2024-12-30 16:11:07

对我来说看起来不错。这就是我使用的(可作为要点

- (NSString *)uuidString {
    // Returns a UUID

    CFUUIDRef uuid = CFUUIDCreate(kCFAllocatorDefault);
    NSString *uuidString = (__bridge_transfer NSString *)CFUUIDCreateString(kCFAllocatorDefault, uuid);
    CFRelease(uuid);

    return uuidString;
}

编辑添加

如果您使用的是 OS X 10.8或者 iOS 6 您可以使用新的 NSUUID 类生成字符串 UUID,无需前往核心基础:

NSString *uuidString = [[NSUUID UUID] UUIDString];
// Generates: 7E60066C-C7F3-438A-95B1-DDE8634E1072

但大多数情况下,如果您只想为文件或目录名称生成唯一的字符串,那么您可以使用 NSProcessInfo 的 globallyUniqueString 方法,例如:

NSString *uuidString = [[NSProcessInfo processInfo] globallyUniqueString];
// generates 56341C6E-35A7-4C97-9C5E-7AC79673EAB2-539-000001F95B327819

它不是正式的 UUID,但对于您的网络和进程来说是唯一的,并且在很多情况下都是不错的选择。

Looks fine to me. This is what I use (available as a gist)

- (NSString *)uuidString {
    // Returns a UUID

    CFUUIDRef uuid = CFUUIDCreate(kCFAllocatorDefault);
    NSString *uuidString = (__bridge_transfer NSString *)CFUUIDCreateString(kCFAllocatorDefault, uuid);
    CFRelease(uuid);

    return uuidString;
}

Edited to add

If you are on OS X 10.8 or iOS 6 you can use the new NSUUID class to generate a string UUID, without having to go to Core Foundation:

NSString *uuidString = [[NSUUID UUID] UUIDString];
// Generates: 7E60066C-C7F3-438A-95B1-DDE8634E1072

But mostly, if you just want to generate a unique string for a file or directory name then you can use NSProcessInfo's globallyUniqueString method like:

NSString *uuidString = [[NSProcessInfo processInfo] globallyUniqueString];
// generates 56341C6E-35A7-4C97-9C5E-7AC79673EAB2-539-000001F95B327819

It's not a formal UUID, but it is unique for your network and your process and is a good choice for a lot of cases.

烟雨凡馨 2024-12-30 16:11:07

这对我来说看起来是正确的。

您拥有 CFRelease 的 uuid,这是您在 CFUUIDCreate() 中负责的,

并且您已将字符串的所有权转移给 ARC,因此编译器知道要释放 uuidStr 在适当的时间。

That looks correct to me.

You have CFRelease'd uuid, which is your responsibility from the CFUUIDCreate()

And you've transferred ownership of the string to ARC, so the compiler knows to release uuidStr at the appropriate time.

偏爱你一生 2024-12-30 16:11:07

来自 clang 文档

(__bridge_transfer T) op 将必须具有不可保留指针类型的操作数强制转换为目标类型,该目标类型必须是可保留对象指针类型。 ARC 将在封闭的完整表达式末尾释放值,并根据本地值进行通常的优化。

所以你做得对。

From clang docs:

(__bridge_transfer T) op casts the operand, which must have non-retainable pointer type, to the destination type, which must be a retainable object pointer type. ARC will release the value at the end of the enclosing full-expression, subject to the usual optimizations on local values.

So you are doing it right.

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