5

I just wanted to know if it's possible to create a subfolder in the NSDocumentDirectory and write data into that created folder, like:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);    
NSString *dirPath =[[paths objectAtIndex:0] stringByAppendingPathComponent:@"TestFolder"];  
NSString *filePath =[dirPath stringByAppendingPathComponent:@"testimage.jpg"];  
[imageData writeToFile:filePath atomically:YES];

Thanks in advance for your support!

Magnus
  • 2,016
  • 24
  • 32
Sean
  • 333
  • 7
  • 19

2 Answers2

5

The writeToFile might fail because the directory does not exist. If it does fail, you could try the NSFileManager class that has a createDirectoryAtPath:attributes: method.

zoul
  • 102,279
  • 44
  • 260
  • 354
  • Thanks a lot, you're right, i had to create the TestFolder with the NSFileManager first. Now it works! – Sean Jan 19 '10 at 16:08
5

The createDirectoryAtPath:attributes: method has been deprecated.

Use createDirectoryAtPath:withIntermediateDirectories:attributes:error: instead.

TALLBOY
  • 1,079
  • 1
  • 10
  • 13