方法代码如下:
- (NSString *)getUniqueStrByUUID
{
CFUUIDRef uuidObj = CFUUIDCreate(nil); //create a new UUID
//get the string representation of the UUID
NSString *uuidString = (NSString *)CFUUIDCreateString(nil, uuidObj);
CFRelease(uuidObj);
return uuidString ;
}
上面方法可能会出现内存泄漏,我在官方的例子AdvancedURLConnections中看到它的实现方法如下:
- (NSString *)pathForTemporaryFileWithPrefix:(NSString *)prefix |
{ |
NSString * result; |
CFUUIDRef uuid; |
CFStringRef uuidStr; |
|
assert(prefix != nil); |
|
uuid = CFUUIDCreate(NULL); |
assert(uuid != NULL); |
|
uuidStr = CFUUIDCreateString(NULL, uuid); |
assert(uuidStr != NULL); |
|
result = [NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"%@-%@", prefix, uuidStr]]; |
assert(result != nil); |
|
CFRelease(uuidStr); |
CFRelease(uuid); |
|
return result; |
} |