views:

142

answers:

1

I am trying to write to the iPhone app's cache folder with images downloaded from a server. My plan is to save the images in "site.com/path/to/image.jpg", since mirroring the server folder structure like that makes duplicate names impossible, and it just seems the right way to do it. Unfortunately, it seems I have to create the folders myself before writing the file. Is there a method in Objective-C to call which, if given "/Users/Me/a/b/c" (no, that's not a valid iPhone path, just an example) will realize /Users/Me exists, and then create a, then under a create b, and so on until it gets to the end?

I could write it myself, but I like to use built-in functions as much as possible. I couldn't find anything about this here.

+7  A: 

Try this:

NSString * path = @"/Users/Me/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/y/z";
NSError * error = nil;
BOOL success = [[NSFileManager defaultManager] createDirectoryAtPath:path 
                                         withIntermediateDirectories:YES 
                                                          attributes:nil 
                                                               error:&error];
if (!success || error) {
  NSLog(@"Error! %@", error);
} else {
  NSLog(@"Success!");
}
Dave DeLong
Thanks so much!