我正在为iphone构建一个应用程序.我扫描iPod库并引入所有专辑封面.我需要一种创建唯一字符串的方法,这样当我将专辑封面保存到文档目录时,每个文件都有一个唯一的名称.有谁知道创建一个独特的字符串?提前致谢.
tho*_*mas 14
我用这个代码:
+ (NSString *)getUniqueFilenameInFolder:(NSString *)folder forFileExtension:(NSString *)fileExtension {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *existingFiles = [fileManager contentsOfDirectoryAtPath:folder error:nil];
NSString *uniqueFilename;
do {
CFUUIDRef newUniqueId = CFUUIDCreate(kCFAllocatorDefault);
CFStringRef newUniqueIdString = CFUUIDCreateString(kCFAllocatorDefault, newUniqueId);
uniqueFilename = [[folder stringByAppendingPathComponent:(NSString *)newUniqueIdString] stringByAppendingPathExtension:fileExtension];
CFRelease(newUniqueId);
CFRelease(newUniqueIdString);
} while ([existingFiles containsObject:uniqueFilename]);
return uniqueFilename;
}
Run Code Online (Sandbox Code Playgroud)
也许它有助于某人.:-)
请记住,这会返回唯一文件的完整路径.您可能希望添加一些小的更改,只返回文件名.例如,如果你想在某个地方坚持它.
小智 5
您希望创建GUID或UUID,它是一个128位整数,其中包含与之关联的字符串.查看http://developer.apple.com/library/ios/#documentation/CoreFoundation/Reference/CFUUIDRef/Reference/reference.html
| 归档时间: |
|
| 查看次数: |
3014 次 |
| 最近记录: |