Saving movie paths in cocoa

itsthejayj's picture

Soz newbie question.

i've got a cocoa app with multiple text fields feeding movie paths into a QC composition i was hoping someone could help me find the best way to save those file path as an external file? or so that when you relaunch my app the text fields will remember the last movie paths used?

Thanks for any help

Jon

cwright's picture
NSFileManager

Your best bet would probably be to use NSFileManager, with something like this:

[[NSFileManager defaultManager] createFileAtPath:path
                                contents:(NSData*)text
                                attributes:nil];

"path" is where the file's located, and "text" is an NSString with the data to put in the file. I haven't tried it myself, but I bet you could put an NSDictionary in place of text (remember to cast it to NSData!), and save multiple strings in the file.

you can then read the contents with contentsAtPath:, which will return the data stored therein.

or you can resort to oldschool posix file o/i, like fopen, fwrite, and fclose. I wouldn't recommend this though.

smokris's picture
or use User Defaults or NSDocument, for additional Cocoa flavor

Chris's suggestion to use NSFileManager is great if you want to store data in a single "configuration file" with a particular name that you specify. If you don't care about the filename and want to let Mac OS X deal with that, you could use the "User Defaults" system, which is really slick --- check out http://developer.apple.com/documentation/Cocoa/Conceptual/UserDefaults/i... . For example, by doing simply:

[[NSUserDefaults standardUserDefaults] setObject:@"YES" forKey:@"DeleteBackup"];

you get a ~/Library/Preferences/application_identifier.plist file containing the data you specified. You can read the data just as easily by doing

[[NSUserDefaults standardUserDefaults] boolForKey:@"DeleteBackup"];

...and this works with any serializable type of data.

However, if you want to have more than a single instance of this data file --- if you want to treat these files as "documents", in that the user can have many of them around and can choose a particular file by double-clicking on it in finder --- you should base your Cocoa application on the NSDocument class. Check out this guide: http://developer.apple.com/documentation/Cocoa/Conceptual/Documents/inde... . Pay close attention to the readFromData:ofType:error: and dataOfType:error: methods.

itsthejayj's picture
Thanks guys for the point in

Thanks guys for the point in the right direction, sounds like a fun week of cocoa down time for me