Create a unique row used to save data.

I am creating an iphone application. I scan the iPod library and list all album art. I need a way to create a unique line so that when saving album art in the document directory, each file has a unique name. Does anyone know to create a unique string? Thanks in advance.

+7
source share
3 answers

You want to create a GUID or UUID, which is a 128-bit integer that has a string associated with it. Check out http://developer.apple.com/library/ios/#documentation/CoreFoundation/Reference/CFUUIDRef/Reference/reference.html

+5
source

I am using this code:

+ (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; } 

Maybe this helps someone. :-)

Keep in mind that this returns the full path to the unique file. You might want to add small changes to simply return the file name. For example, if you want to save it somewhere.

+14
source

I donโ€™t have the code, but you can create the MD5 hash code from the album cover. This will be close to a unique line. That would be an ugly line.

0
source

All Articles