CFUUIDRef和CFStringRef-生成唯一标识符
一般在项目中,我们要生成一个不重复的唯一的表示符作为文件或者路径名称的时候,可以使用CFUUIDRef
和CFStringRef来生成一个唯一的标识:
代码如下:
- (NSString *)createCUID:(NSString *)prefix{ NSString * result; CFUUIDRef uuid; CFStringRef uuidStr; uuid = CFUUIDCreate(NULL); uuidStr = CFUUIDCreateString(NULL, uuid); result =[NSString stringWithFormat:@"%@-%@", prefix,uuidStr]; CFRelease(uuidStr); CFRelease(uuid); return result; }
或
NSString * get_uuid() { CFUUIDRef uuid_ref = CFUUIDCreate(NULL); CFStringRef uuid_string_ref= CFUUIDCreateString(NULL, uuid_ref); CFRelease(uuid_ref); NSString *uuid = [NSString stringWithString:(__bridge NSString*)uuid_string_ref]; CFRelease(uuid_string_ref); return uuid; }