Strange problem with reading and writing a plist file

╄→尐↘猪︶ㄣ 提交于 2019-12-04 16:33:33

You can't write back to the application bundle. You will have to copy the original plist file to the documents directory or any other writable location before it can be written to.

An example

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString libraryPath = [paths objectAtIndex:0];
NSString plistPath = [libraryPath stringByAppendingPathComponent:@"settings.plist"];

// Checks if the file exists at the writable location.
if ( ![[NSFileManager defaultManager] fileExistsAtPath:plistPath] ) {
    NSString *masterFilePath = [[NSBundle mainBundle] pathForResource:@"settings" ofType:@"plist"];

    // Try to copy the master file to the writable location
    NSError *error;
    if ( ![[NSFileManager defaultManager] copyItemAtPath:masterFilePath toPath:plistPath error:&error] ) {
        NSLog(@"Error: %@", [error localizedDescription]); 
        // Serious error.
    }
}

...
// Ready for use.
NSData *plistData = [NSData dataWithContentsOfFile:plistPath];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!