"nsfilemanager unique file names" Code Answer

2

create your own file name:

cfuuidref uuid = cfuuidcreate(null);
cfstringref uuidstring = cfuuidcreatestring(null, uuid);
cfrelease(uuid);
nsstring *uniquefilename = [nsstring stringwithformat:@"%@%@", prefixstring, (nsstring *)uuidstring];
cfrelease(uuidstring);

a simpler alternative proposed by @darrinm in the comments:

nsstring *prefixstring = @"myfilename";

nsstring *guid = [[nsprocessinfo processinfo] globallyuniquestring] ;
nsstring *uniquefilename = [nsstring stringwithformat:@"%@_%@", prefixstring, guid];

nslog(@"uniquefilename: '%@'", uniquefilename);

nslog output:
uniquefilename: 'myfilename_680e77f2-20b8-444e-875b-11453b06606e-688-00000145b460af51'

note: ios6 introduced the nsuuid class which can be used in place of cfuuid.

nsstring *guid = [[nsuuid new] uuidstring];
By Ransaka Ravihara on September 1 2022
Only authorized users can answer the Search term. Please sign in first, or register a free account.