views:

47

answers:

1

Hi,

I want to generate unique file paths in Objective-C. I looked at NSTemporaryDirectory() but the closest I could get was to this and this. But in the second one they create such a file. I do not want to create it, I just want an NSString for a unique writable path... I don't need paths to folders, just files. How is this possible?

A: 

You can create file name using present system date and time ([[NSCalendarDate calendarDate] descriptionWithCalendarFormat:@"%m%d%Y%H%M%S%F"]) .... this include even milliseconds ... use returning string as unique file name ....

Read more about date formates -> http://developer.apple.com/iphone/library/documentation/cocoa/Conceptual/DatesAndTimes/Articles/LegacyNSCalendarDate.html

Girish Kolari
Yes, I can, but who says they're unique? Perhaps another app has already a file with the same name there.
Michael
There are deferent approaches to handle above mentioned issue1. Have folder with your appName where you want to create a file and keep file inside it.(if you are in iPhone app is in sandbox no issue)2. Have prefix for file name <AppNAME>_<FileName> (you can have app name or some constant string may be a HASH code)3. Check is there a file of your choice if there is one then generate one more time, since you get different name in every micro second you should get a unique name with some number of iteration of try.4.Even you can have combination all 3 option for better result
Girish Kolari