Vincent Gable’s Blog

February 24, 2008

UUID (GUID) Support in Cocoa

Filed under: Cocoa,Objective-C,Programming,UNIX | , ,
― Vincent Gable on February 24, 2008

To easily create a new UUID (aka GUID) in Cocoa, add the following method as a category to NSString:

+ (NSString*) stringWithUUID {
   CFUUIDRef uuidObj = CFUUIDCreate(nil);//create a new UUID
   //get the string representation of the UUID
   NSString *uuidString = (NSString*)CFUUIDCreateString(nil, uuidObj);
   CFRelease(uuidObj);
   return [uuidString autorelease];
}

That’s it, just 4 lines of code. There’s really no need to deal with an entire UUID class. All you ever need to do with UUIDs is create them, compare them, and store/retrieve them. + stringWithUUID handles creation. The string it returns responds to - isEqual:, and is easy to write or read using the mature Foundation APIs. What more could you ask for from a UUID object?

Simpler Ways:

You might only need: [[NSProcessInfo processInfo] globallyUniqueString]. As the name implies, it gives you a UUID that can be used to identify a process.

If you want a UUID without messing with compiled code, the uuidgen command-line tool generates them, or you can use this website.

10 Comments »

  1. thanks so much. This not only got me what I needed but familiarized me with the UUID class and appropriately attaching categories

    Comment by Rusty — September 4, 2008 @ 5:56 pm

  2. In accordance with the Create Rule, it should really be called “stringWithRandomUUID” or something else– it’s autoreleased, but “New” in the name would imply that the caller owns the object.

    Comment by Adam — November 24, 2008 @ 8:11 pm

  3. Good catch Adam, I’ve changed the name, thanks!

    Comment by Vincent Gable — November 24, 2008 @ 9:49 pm

  4. Thanks for this nugget!

    Comment by Pink — January 25, 2012 @ 1:19 am

  5. […] UUID(GUID) Support in Cocoa |2|left […]

    Pingback by UUID??? · Linunio — May 13, 2012 @ 11:06 pm

  6. As a food, coconut is tasty, sweet, & highly nutritious. Besides
    chocolate, you may also effortlessly put coconut oil to just about any of your dishes.
    When it comes to antibacterial properties, one study in particular found that virgin coconut
    oil helped to treat skin infections.

    Comment by Nan — April 26, 2013 @ 5:30 am

  7. It’s an amazing paragraph in favor of all the internet people; they will get benefit from it I am sure.

    Comment by www.lindenchamber.Net — May 8, 2013 @ 6:13 pm

  8. Hi there, for all time i used to check website posts here
    in the early hours in the dawn, since i like to gain knowledge of more and more.

    Comment by flower 8 4inches japanese — October 30, 2014 @ 12:02 pm

  9. I take pleasure in, result in I discovered exactly what I was having a look for.
    You’ve ended my four day long hunt! God Bless you
    man. Have a great day. Bye

    Comment by curl dobby button — October 30, 2014 @ 3:46 pm

  10. certainly like your web site however you need to test the
    spelling on quite a few of your posts. Several of them are rife with spelling issues and
    I find it very troublesome to tell the reality then again I’ll certainly come back again.

    Comment by auxiliary blackberry brand black — May 10, 2015 @ 10:44 pm

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress