1. 程式人生 > >隨即字串的生成 & 利用udid 生成 唯一的字串 appid

隨即字串的生成 & 利用udid 生成 唯一的字串 appid

方法1

// 利用udid 生成 唯一的字串 appid

-(NSString*) stringWithUUID

{

CFUUIDRef uuidObj = CFUUIDCreate(kCFAllocatorDefault);

CFStringRef strRef = CFUUIDCreateString(kCFAllocatorDefault, uuidObj);

NSString* appid = [NSStringstringWithString:(NSString*)strRef];

CFRelease(strRef);

CFRelease(uuidObj);

uuidString = [uuidString stringByReplacingOccurrencesOfString:@"-"

withString:@""];

return appid;

}

方法2

-(NSString*) stringWithUUID:(int)len

{

NSString *appid = @"abcdefghigklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

    NSMutableString *randomString = [NSMutableString stringWithCapacity: len];

    for (int i=0; i<len; i++) {

        [randomString appendFormat: @"%C", [appid characterAtIndex: arc4random() % [appid length]]];

    }

return appid;

}