views:

424

answers:

3

I have found several snippets of code describing how to write data to a user's application Documents folder. However, when I try this out in the iPhone simulator, no files get created. I called

[NSFileManager isWritbleAtPath:<my document folder>]

and it returned 0 (false). Do I need to make this folder explicitly writable, and if so, how do I do it?

+1  A: 

Hi Casademora,

The iPhone simulator should be able to write to the entire disk. My app routinely dumps test files to the root level of my boot volume (using [NSData's writeToPath:@"/test.jpg" atomically:NO]).

Are you sure that you've correctly determined the path to the documents folder? You need to expand the tilde in the path. Here's the code my app uses to put things in the documents folder. I don't think there's any more setup involved!

brushesDir = [[@"~/Documents/BrushPacks/" stringByExpandingTildeInPath] retain];
// create brush packs folder if it does not exist
if (![[NSFileManager defaultManager] fileExistsAtPath: brushesDir])
            [[NSFileManager defaultManager] createDirectoryAtPath:brushesDir withIntermediateDirectories:YES attributes:nil error:nil];
Ben Gotow
I suspected as much. I'll give this a try. Thanks!
casademora
+1  A: 
NSLog(@"writable: %d", [[NSFileManager defaultManager] isWritableFileAtPath:NSHomeDirectory()]);

This prints 1 on the console.

Did you mean to call the method isWritableAtPath or isWritableFileAtPath ? And did you mean to call it on the class itself, or on a (default) instance of it?

drvdijk
I tried this, and it returns 0. This is has me wondering what I'm doing wrong
casademora
A: 

Thanks for the pointers. So after a toiling through a few documents, I found the thing I was doing wrong: trying to save an NSArray that wasn't composed of basic datatypes such as NSDictionary, NSArray, or NSString. I was trying to save an array of MPMediaItems (from the MediaKit Framework in SDK 3.0+).

casademora