I am trying to create a plist file that I will write to later in my app. In the first viewDidLoad I call the following method
-(void)createFavoritesFile{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *documentDBFolderPath = [documentsDirectory stringByAppendingPathComponent:@"data.plist"];
if (![fileManager fileExistsAtPath:documentDBFolderPath])
{
NSLog(@"file doesnt exist");
NSString *resourceDBFolderPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"data.plist"];
[fileManager copyItemAtPath:resourceDBFolderPath toPath:documentDBFolderPath error:&error];
}
else{
NSLog(@"file exists");
}
}
However every time I run the app, I cannot find a file created, and if I close the app and reopen, NSLog shows that file doesnt exist again. Am I missing something?
You’re ignoring the return value and error from
-copyItemAtPath:toPath:error:. I’m willing to bet that call is returningNOand populating some error. You should check the return value of that and print out the error if it returnsNO.One possible reason why this is failing is you might not actually have a Documents folder yet, for some reason. You can ask
NSFileManagerto create it for you.