开发者

How to generate unique GUID 36 chars in SQLITE iPhone

I am trying to create a table which is unique and has primary key. I k开发者_开发技巧now in sqlite we can develop Unique AUTOINCREMENT ID SQL AUTOINCREMENT, but is it possible to generate Unique GUID which is 36 chars long. The only reason to do that is to make it more unique.


This is the bit of code I use for UUIDs (I may have even found it here on Stack Overflow)...

+ (NSString *)GetUUID
{
    CFUUIDRef theUUID = CFUUIDCreate(NULL);
    CFStringRef string = CFUUIDCreateString(NULL, theUUID);
    CFRelease(theUUID);
    return [(NSString *)string autorelease];
}

I don't know how long the UUIDs generated are because in the ways I use it I don't care so perhaps check that by passing the result into a NSLog call.

HTH, Pedro :)


I user this code to generate guids on the iphone - category on NSString. Can't remember where I found it, but it works great.

#import "NSString_UniqueID.h"


static unichar x (unsigned int); 

@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 '_'; 
} 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜