Generate a UUID string with ARC enabled

Objective CAutomatic Ref-Counting

Objective C Problem Overview


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?

Objective C Solutions


Solution 1 - Objective C

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.

Solution 2 - Objective C

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.

Solution 3 - Objective C

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAbhi BeckertView Question on Stackoverflow
Solution 1 - Objective CAbizernView Answer on Stackoverflow
Solution 2 - Objective CFiroze LafeerView Answer on Stackoverflow
Solution 3 - Objective CiHunterView Answer on Stackoverflow