Writing to files in bundle?

cocoa, objective-c, xcode

Solution

You can write files to the application bundle as much as you'd like. Just get the path of the file through NSBundle's `pathForResource:ofType:` method and you can write to that file. If you want just the directory of the bundle, use `resourcePath`.

You don't want to do this, though. There are various reasons, but you'll break code signing, which is a big one. You should use the established conventions instead (such as writing to Library:Application Support:Your App).

EDIT: For a (possibly) more convincing reason of not to do this... When I was first learning Cocoa programming, I saved to the bundle because I didn't want to bother with the Library. It got really annoying, though, because every time you make a change to your program, you lose all of your saved data/settings for that program (assuming you're not using `NSUserDefaults` for preferences). I kept having to move it over from the old version to the new one. By using the Library, you don't have to worry about this.

Problem

If you scroll down to the section 'Writing to Files and URLs' at this link, would the path varaible have to be a file on disk? Is it possible to write to a file in the bundle or must it always be deployed first?

Original source