Skip to content

Instantly share code, notes, and snippets.

@Kozlov-V
Created February 15, 2015 12:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kozlov-V/df05859f14ea29888368 to your computer and use it in GitHub Desktop.
Save Kozlov-V/df05859f14ea29888368 to your computer and use it in GitHub Desktop.
Generate UUID xcode random
+(NSString *) generateUUID
{
CFUUIDRef uuid = CFUUIDCreate(kCFAllocatorDefault);
CFStringRef uuidString = CFUUIDCreateString(kCFAllocatorDefault, uuid);
CFRelease(uuid);
NSString* newUuid = [(NSString *)uuidString copy];
CFRelease(uuidString);
return [newUuid autorelease];
}
Пример сгенеренной строки:
CAEFED6E-47E8-4021-A367-FE98807D4F14
///////////////////////////////////////////////////////////////////////////////////////////
NSString *uuidString = [[NSProcessInfo processInfo] globallyUniqueString];
// generates 56341C6E-35A7-4C97-9C5E-7AC79673EAB2-539-000001F95B327819
///////////////////////////////////////////////////////////////////////////////////////////
// This category uses Apple's core foundation to generate a 128-bit universal id and then
// encodes the bits into a 22 character string. The conventional string form of a universal
// id is 36 characters long, so this saves 14 characters.
//----------------------------------------------------------------------------------------
@implementation NSString (TWUUID)
+ (NSString*) stringWithUniqueId
{
CFUUIDRef uuid = CFUUIDCreate(NULL);
CFUUIDBytes b = CFUUIDGetUUIDBytes(uuid);
unichar unichars[22];
unichar* c = unichars;
*c++ = x(b.byte0 >> 2);
*c++ = x((b.byte0 & 3 << 4) + (b.byte1 >> 4));
*c++ = x((b.byte1 & 15 << 2) + (b.byte2 >> 6));
*c++ = x(b.byte2 & 63);
*c++ = x(b.byte3 >> 2);
*c++ = x((b.byte3 & 3 << 4) + (b.byte4 >> 4));
*c++ = x((b.byte4 & 15 << 2) + (b.byte5 >> 6));
*c++ = x(b.byte5 & 63);
*c++ = x(b.byte6 >> 2);
*c++ = x((b.byte6 & 3 << 4) + (b.byte7 >> 4));
*c++ = x((b.byte7 & 15 << 2) + (b.byte8 >> 6));
*c++ = x(b.byte8 & 63);
*c++ = x(b.byte9 >> 2);
*c++ = x((b.byte9 & 3 << 4) + (b.byte10 >> 4));
*c++ = x((b.byte10 & 15 << 2) + (b.byte11 >> 6));
*c++ = x(b.byte11 & 63);
*c++ = x(b.byte12 >> 2);
*c++ = x((b.byte12 & 3 << 4) + (b.byte13 >> 4));
*c++ = x((b.byte13 & 15 << 2) + (b.byte14 >> 6));
*c++ = x(b.byte14 & 63);
*c++ = x(b.byte15 >> 2);
*c = x(b.byte15 & 3);
CFRelease(uuid);
return [NSString stringWithCharacters: unichars length: 22];
}
@end
// Convert six-bit values into letters, numbers or _ or $ (64 characters in that set).
//------------------------------------------------------------------------------------
unichar x (unsigned int c)
{
if (c < 26) return 'a' + c;
if (c < 52) return 'A' + c - 26;
if (c < 62) return '0' + c - 52;
if (c == 62) return '$';
return '_';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment