将plist复制到文档目录

xon*_*rlz 3 iphone objective-c ipad

如何将应用程序构建中的现有plist复制到Document目录?

我的plist基本上是一个字典数组,我已经使用以下代码成功地将plist复制到目录:

 BOOL success;
            NSError *error;

            NSFileManager *fileManager = [NSFileManager defaultManager];
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString *documentsDirectory = [paths objectAtIndex:0];
            NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"States.plist"];

            success = [fileManager fileExistsAtPath:filePath];
            if (success) return;

            NSString *path = [[NSBundle mainBundle] pathForResource:@"States" ofType:@"plist"];    
            success = [fileManager copyItemAtPath:path toPath:filePath error:&error];

            if (!success) {
                NSAssert1(0, @"Failed to copy Plist. Error %@", [error localizedDescription]);
            } 
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试访问NSDictionary时,它给了我:

CoreAnimation: ignoring exception: -[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object
Run Code Online (Sandbox Code Playgroud)

为什么是这样?我必须重新启动应用程序,现在我可以更改存储在我的数组中的字典

Ngu*_*inh 5

这是我将*.plist文件复制到文档文件夹的方法.希望这可以帮助.

+ (NSString*) getPlistPath:(NSString*) filename{
    //Search for standard documents using NSSearchPathForDirectoriesInDomains
    //First Param = Searching the documents directory
    //Second Param = Searching the Users directory and not the System
    //Expand any tildes and identify home directories.
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];
    return [documentsDir stringByAppendingPathComponent:filename];
}
+ (void) copyPlistFileToDocument{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSString *dbPath = [Utilities getPlistPath:@"AppStatus.plist"];
    if( ![fileManager fileExistsAtPath:dbPath])
    {
        NSString *defaultDBPath = [[NSBundle mainBundle] pathForResource:@"AppStatus" ofType:@"plist"];
            BOOL copyResult = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];
            if(!copyResult)
                    NSAssert1(0, @"Failed to create writable plist file with message '%@'.", [error localizedDescription]);
    }

}
Run Code Online (Sandbox Code Playgroud)