1

I'm writing an app that needs to store some persistence information in file but i'm having trouble debugging the createFileAtPath:contents:attributes: function of NSFileManager, i have reduced the problem to the below piece of code.

NSFileManager *filemgr;
filemgr = [NSFileManager defaultManager];
NSString * fileName = @"newfile.txt";

NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *filePath = [documentsDirectory stringByAppendingPathComponent: fileName];
NSLog(@"full path name: %@", filePath);

// check if file exists
if ([filemgr fileExistsAtPath: filePath] == YES){
    NSLog(@"File exists");

}else {
    NSLog (@"File not found, file will be created");
    if (![filemgr createFileAtPath:filePath contents:nil attributes:nil]){
        NSLog(@"Create file returned NO");
    }
}

Since the function doesn't take an NSError object i'm having trouble finding out why exactly it is returning false.

From other examples i have seen here Write a file on iOS and here How to programmatically create an empty .m4a file of a certain length under iOS using CoreAudio on device? it should be ok to pass nil to both the contents and attributes parameters. I've tried specifying both parameters and receive the same result.

I'm thinking this may be a permissions issue but i'm unsure if the below code is the correct way to check permissions on a directory.

if ([filemgr isWritableFileAtPath:documentsDirectory] == YES){
    NSLog (@"File is readable and writable");
}
Community
  • 1
  • 1
Sirhc
  • 518
  • 7
  • 13
  • do you have this problem on both your testing device and in the simulator, or only in one or the other? – Michael Dautermann May 19 '13 at 17:35
  • Only in the simulator at this point, i do not have a testing device. Though i would be curious to know if anyone else receives the same output from the above code. – Sirhc May 19 '13 at 17:39
  • 1
    I've just run the code, and it works perfectly. First time through: "File not found, file will be created", there's no error. Second time: "File exists". – Steve Waddicor May 19 '13 at 17:49
  • Thanks steve, not sure what is going wrong on my end then. – Sirhc May 19 '13 at 17:57

2 Answers2

6
//  Following Code will create Directory in DocumentsDirectory

NSString *docDir = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString *dirName = [docDir stringByAppendingPathComponent:@"MyDir"];

BOOL isDir
NSFileManager *fm = [NSFileManager defaultManager];
if(![fm fileExistsAtPath:dirName isDirectory:&isDir])
{
    if([fm createDirectoryAtPath:dirName withIntermediateDirectories:YES attributes:nil error:nil])
        NSLog(@"Directory Created");
    else
        NSLog(@"Directory Creation Failed");
}
else
    NSLog(@"Directory Already Exist");
Box Box Box Box
  • 5,094
  • 10
  • 49
  • 67
Dipen Panchasara
  • 13,480
  • 5
  • 47
  • 57
  • Thanks, this was the problem, i assumed that the path returned by NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] would exist, evidently this is not always the case in the simulator. – Sirhc May 20 '13 at 08:43
1

swift5

guard let docdir = NSSearchPathForDirectoriesInDomains(.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).first else {
            preconditionFailure()
}
let path = (docdir as NSString).appendingPathComponent(yourfilename + "." + yourfilenameExtension)
Anton Tropashko
  • 5,486
  • 5
  • 41
  • 66