| Index | Submit class | Submit snippet | Submission feed | List |

Snippet #45

Language: Objective-C, Author: Andy Matuschak
License: Public domain

GetUUID(): Quickly generate a UUID using Core Foundation.

NSString *GetUUID()
{
  return (NSString *)CFUUIDCreateString(kCFAllocatorDefault, CFUUIDCreate(kCFAllocatorDefault));
}

Compatible with:


Comments

Zach S. says:

Sorry, but this snippet leaks memory; the reference to the UUID object returned by CFUUIDCreate is lost after this function returns and can never be released.

A Cocoa-friendly implementation could involve a category on NSString:

+ (NSString*)UUIDString {
CFUUIDRef uuidRef = CFUUIDCreate(kCFAllocatorDefault);
CFStringRef stringRef = CFUUIDCreateString(kCFAllocatorDefault, uuidRef);
CFRelease(uuidRef);

return [(id)stringRef autorelease];
}

Name

Website

Do you hate spammers? (Answer "Yes")