Is there a method to generate a standard 128bit GUID (UUID) on the Mac?

Is there a built in function equivalent to .NET's


in Cocoa?

My desire is to produce a string along the lines of 550e8400-e29b-41d4-a716-446655440000 which represents a unique identifier.

-------------Problems Reply------------

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.

Some code:

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

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

if you really want the bytes (not the string):

+(CFUUIDBytes)UUIDBytes {
CFUUIDBytes bytes = CFUUIDGetUUIDBytes(theUUID);
return bytes;

where CFUUIDBytes is a struct of the UUID bytes.

At least on MacOSX 10.5.x you might use the command line tool "uuidgen" to get your string e.g.

$ uuidgen


there's also an option -hdr with this comand that conveniently generates it in header style

See man-page for further infos.

or there's the uuidgen command line tool.

Since 10.8 you could also use: NSString *uuidString = [[NSUUID UUID] UUIDString];

Check out the Wikipedia article and the Core Foundation page.

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.

Category:cocoa Views:0 Time:2008-11-15

Related post

Copyright (C), All Rights Reserved.

processed in 0.147 (s). 11 q(s)