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?
Asked
Active
Viewed 1,257 times
2
1 Answers
1
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
- 2,515
- 2
- 24
- 34
-
Yes, I can, but who says they're unique? Perhaps another app has already a file with the same name there. – Michael Jul 21 '10 at 10:42
-
There are deferent approaches to handle above mentioned issue 1. 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
_ – Girish Kolari Jul 21 '10 at 14:25(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