Is there a method to generate a standard 128bit GUID (UUID) on the Mac?
Solution 1
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.
Solution 2
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.
Solution 3
Since 10.8 you could also use:
NSString *uuidString = [[NSUUID UUID] UUIDString];
Solution 4
Check out the Wikipedia article and the Core Foundation page.
Solution 5
You can create an alias in the ~/.bash_profile
and call uuid
from the terminal:
alias uuid="uuidgen | tr '[:upper:]' '[:lower:]'"
Redwood
I'm a San Francisco Bay Area programmer working in Objective-C/Cocoa and C#/.NET.
Updated on January 14, 2021Comments
-
Redwood over 3 years
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.