Reputation: 1605
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?
Upvotes: 0
Views: 495
Reputation: 5079
Print your resource path and see if the file exists in there. If so, but not working check this question: Working with paths from [[NSBundle mainBundle] resourcePath]
Anyway, if your resource is in your main bundle you can always use:
- (NSString *)pathForResource:(NSString *)name ofType:(NSString *)extension
Upvotes: 0
Reputation: 185671
You're ignoring the return value and error from -copyItemAtPath:toPath:error:
. I'm willing to bet that call is returning NO
and populating some error. You should check the return value of that and print out the error if it returns NO
.
One possible reason why this is failing is you might not actually have a Documents folder yet, for some reason. You can ask NSFileManager
to create it for you.
Upvotes: 1