RSS

Making UUID’s in Cocoa

If you haven’t heard of Universally Unique Identifiers or (UUIDs), they are a way to make a unique identifier for anything (possibly your database entries) from different locations without having to use a central server to create unique identifiers. As an example, you might want to create a unique identifier of your user inside your Cocoa app that will never clash with any other user’s unique ID. You could create your own server and write a script that would generate a unique ID for each client, but this is a centralized approach and requires a network connection, is slower, and involves processing time on your server. Instead you can use UUID’s, which are created on the client machine.

To do this in Cocoa is fairly easy. We just need to drop down to the Carbon API’s, which can be difficult for beginners. Here’s how:

1
2
3
4
5
CFUUIDRef uuidRef = CFUUIDCreate(kCFAllocatorDefault);
CFStringRef strRef = CFUUIDCreateString(kCFAllocatorDefault, uuidRef);
NSString *uuidString = [NSString stringWithString:(NSString*)strRef];
CFRelease(strRef);
CFRelease(uuidRef);

Very simply, this creates a UUID, turns it into a string, and turns that Core Foundation string into an NSString. It then releases the memory of those two carbon objects. You will note that we just cast strRef as an NSString. This is toll-free bridging between CF and Cocoa going to work for you. If you have any questions or comments please feel free to comment below.

  • Oliver Beattie

    Glad it worked out :)

  • Preston

    No need for the +stringWithString method:

    NSString *uuidString = (NSString *)CFUUIDCreateString(kCFAllocatorDefault, uuidRef);

  • http://www.air-jordan-16.com air jordan 16

    Nike Zoom Blur Low
    Yeah, I have to admire the landlord's unique point of view, this article is very comprehensive and considerable on the analyse, and greatly inspired me. In addition, I would like to share that some other blog's article, content is also very good, if you scan it,there will be a suprise!

  • Guest

    You can do this in Cocoa with:

    NSString *UUID = [[NSProcessInfo processInfo] globallyUniqueString];

  • http://www.cozyfivefingersshoes.com five fingers shoes

    Love everything about it. I love to talk about it. I love to dream about it. I see the wonderful inspiring photos online and wish I can be the one taking it. yeah, that’s my ultimate dream.

  • Christian Louboutin Outlet

    Christian  Louboutin Shoes was in beautiful looking and fashion in design.Christian  Louboutin Outlet is becoming more and more popular in the world.We have lots of  Cheap Christian Louboutin Shoes,we promise you will fall in love,Moncler  Outlet online shopping store is free shipping and no tax  worldwide for you.Come and join us to order. season for you,We will provide the  most top quality Moncler products and the best service for you.

  • http://www.nbashoessale-us.com/ nba shoes sale

    Chinese Embassy in Mali has issued an urgent warning to require attention to safety precautions in Mali funded institutions and overseas Chinese, concerned about the development of the situation, avoid going out to avoid near the Presidential Palace, Cutty barracks, the Defense Department and other sensitive sites.pingdeshijieluanle852

« Awful Experience Canceling Powweb Account | Mashduo 1.0 Released »