Search code examples
cocoansfilemanager

Why is NSFileManager not working?


I've written some code to copy a file (dbtemplate.sqlite) from the application package to the library. However, no file shows up in the library and every time I start the application it logs the text that it copied the template. There are no errors showing up in the console. What am I doing wrong?

NSFileManager *fileManager = [NSFileManager defaultManager];

if (![fileManager fileExistsAtPath:@"~/Library/AppSafe/database/db.sqlite"]) {

    [fileManager createDirectoryAtPath:@"~/Library/AppSafe/database" withIntermediateDirectories:YES attributes:nil error:nil];

    [fileManager copyItemAtPath:@"dbtemplate.sqlite" toPath:@"~/Library/AppSafe/database/db.sqlite" error:nil];
    NSLog(@"copied template");

}

Solution

  • If I remember correctly, you have to pass a full path into the NSFileManager methods, and using a tilde-prefixed path won't work.

    So instead of using @"~/Library/...", use:

    [@"~/Library/..." stringByExpandingTildeInPath]