{"id":3,"date":"2008-02-24T22:28:08","date_gmt":"2008-02-24T22:28:08","guid":{"rendered":"http:\/\/vgable.com\/blog\/2008\/02\/24\/creating-a-uuid-guid-in-cocoa\/"},"modified":"2008-11-24T21:42:38","modified_gmt":"2008-11-25T02:42:38","slug":"creating-a-uuid-guid-in-cocoa","status":"publish","type":"post","link":"https:\/\/vgable.com\/blog\/2008\/02\/24\/creating-a-uuid-guid-in-cocoa\/","title":{"rendered":"UUID (GUID) Support in Cocoa"},"content":{"rendered":"<p>To easily create a new <a href=\"http:\/\/www.faqs.org\/rfcs\/rfc4122.html\">UUID<\/a> (aka <a href=\"http:\/\/en.wikipedia.org\/wiki\/Globally_Unique_Identifier\">GUID<\/a>) in Cocoa, add the following method as a category to NSString:<\/p>\n<p><code>+ (NSString*) stringWithUUID {<br \/>\n&nbsp;&nbsp;&nbsp;CFUUIDRef\tuuidObj = CFUUIDCreate(nil);\/\/create a new UUID<br \/>\n&nbsp;&nbsp;&nbsp;\/\/get the string representation of the UUID<br \/>\n&nbsp;&nbsp;&nbsp;NSString\t*uuidString = (NSString*)CFUUIDCreateString(nil, uuidObj);<br \/>\n&nbsp;&nbsp;&nbsp;CFRelease(uuidObj);<br \/>\n&nbsp;&nbsp;&nbsp;return [uuidString autorelease];<br \/>\n}<\/code><\/p>\n<p>That&#8217;s it, just 4 lines of code.\u00a0There&#8217;s really no need to deal with an entire UUID <i>class<\/i>. All you ever need to do with UUIDs is create them, compare them, and store\/retrieve them.  <code>+ stringWithUUID<\/code> handles creation.  The string it returns responds to <code> - isEqual:<\/code>, and is easy to write or read using the mature Foundation APIs.  What more could you ask for from a UUID object?<\/p>\n<h3>Simpler Ways:<\/h3>\n<p>You might only need:<code> [[NSProcessInfo processInfo] globallyUniqueString]<\/code>.  As the name implies, it gives you a UUID that can be used to identify a process.<\/p>\n<p>\nIf you want a UUID without messing with compiled code, the <a href=\"x-man-page:\/\/uuidgen\"><code>uuidgen<\/code><\/a> command-line tool generates them, or you can use <a href=\"http:\/\/www.famkruithof.net\/uuid\/uuidgen\">this website<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To easily create a new UUID (aka GUID) in Cocoa, add the following method as a category to NSString: + (NSString*) stringWithUUID { &nbsp;&nbsp;&nbsp;CFUUIDRef uuidObj = CFUUIDCreate(nil);\/\/create a new UUID &nbsp;&nbsp;&nbsp;\/\/get the string representation of the UUID &nbsp;&nbsp;&nbsp;NSString *uuidString = (NSString*)CFUUIDCreateString(nil, uuidObj); &nbsp;&nbsp;&nbsp;CFRelease(uuidObj); &nbsp;&nbsp;&nbsp;return [uuidString autorelease]; } That&#8217;s it, just 4 lines of code.\u00a0There&#8217;s really [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,5,4,9],"tags":[234,233,232],"class_list":["post-3","post","type-post","status-publish","format-standard","hentry","category-cocoa","category-objective-c","category-programming","category-unix","tag-categories","tag-guid","tag-uuid"],"_links":{"self":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts\/3","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/comments?post=3"}],"version-history":[{"count":0,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts\/3\/revisions"}],"wp:attachment":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/media?parent=3"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/categories?post=3"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/tags?post=3"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}