Can you please tell me which is the right way and why in non ARC world.
+ (NSString *)getUUID {
CFUUIDRef theUUID = CFUUIDCreate(NULL);
CFStringRef string = CFUUIDCreateString(NULL, theUUID);
CFRelease(theUUID);
return [(NSString*) string autorelease];
}
or
+ (NSString *)getUUID {
CFUUIDRef theUUID = CFUUIDCreate(NULL);
CFStringRef string = CFUUIDCreateString(NULL, theUUID);
CFRelease(theUUID);
return (NSString*)string;
}
The other answers are correct for manual retain counting. When you come to your senses ;^) and switch to ARC, you won't be able to send autorelease. Instead, under ARC, do it this way:
+ (NSString *)getUUID {
CFUUIDRef theUUID = CFUUIDCreate(NULL);
CFStringRef string = CFUUIDCreateString(NULL, theUUID);
CFRelease(theUUID);
return CFBridgingRelease(string);
}
A CFBridgingRelease is equivalent to a CFRelease for the purposes of balancing the +1 retain count returned by CFUUIDCreateString, but also returns a still-valid reference that ARC will take care of releasing.
CFStrings do need to be released. The first way is correct because CFString is toll-free bridged with NSString, and thus can safely be autoreleased like an NSString.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With