将NSString添加到.plist(属性列表)文件

时间:2013-10-18 11:00:10

标签: ios objective-c plist

我试过阅读很多问题/教程,但我找不到答案! 我正在尝试从textField获取一个字符串并将其保存在我创建的datas.plist文件中(在默认键Root下) 我试过这段代码:

//Save Name to plist
        if([name.text length] > 0){
            NSString *string = name.text;
            [array addObject:string];
            //plist path
            NSString *path = [[NSBundle mainBundle] pathForResource:@"datas" ofType:@"plist"];
            //save object
            [array writeToFile: path atomically:YES];

        }

其中array是

@interface DetailViewController (){
    NSMutableArray* array;
}

但是当我尝试保存时,应用程序崩溃(之前我写过的动作连接到按钮)会出现此错误:

  

* 由于未捕获的异常'NSInvalidArgumentException'而终止应用,原因:'无法创建   NSPersistentStoreCoordinator与nil模型'

我认为写入的代码不正确,因为我从不输入密钥保存位置(如@“Root”)或类似的东西。 提前致谢

修改

我试过这段代码,但没有写任何东西:

- (void)addToMyPlist {
    NSString *destPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    destPath = [destPath stringByAppendingPathComponent:@"dati"];

    // If the file doesn't exist in the Documents Folder, copy it.
    NSFileManager *fileManager = [NSFileManager defaultManager];

    if (![fileManager fileExistsAtPath:destPath]) {
        NSString *sourcePath = [[NSBundle mainBundle] pathForResource:@"dati" ofType:@"plist"];
        [fileManager copyItemAtPath:sourcePath toPath:destPath error:nil];
    }

    // Load the Property List.
    array = [[NSMutableArray alloc] initWithContentsOfFile:destPath];

        //NSString *path = [[NSBundle mainBundle] pathForResource:@"dati" ofType:@"plist"];
        NSString *string = self.name.text;

        NSArray *values = [[NSArray alloc] initWithObjects:string, nil];
        NSArray *keys = [[NSArray alloc] initWithObjects:@"Root", nil];
        NSDictionary *dict = [[NSDictionary alloc] initWithObjects:values forKeys:keys];
        [array addObject:dict];
        [array writeToFile:destPath atomically:YES];
}

出了什么问题?

1 个答案:

答案 0 :(得分:1)

捆绑包中的所有文件都是只读的。

您必须将您的pList移动​​到您的应用程序的私人文档(或另一个,其中有3个可用)文件夹中才能拥有写入权限。

例如,将您的包文件复制到Documents目录:

if([name.text length] > 0){
        NSString *string = name.text;
        [array addObject:string];

        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSError *error;
        //Path to document directory
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];

        //Path to your datas.plist inside documents directory
        NSString *documentsPath = [documentsDirectory stringByAppendingPathComponent:@"datas.plist"];

        //If the file doesn't exists yet
        if ([fileManager fileExistsAtPath:documentsPath] == NO) {
            //Let's copy it in your Documents folder
            NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"datas" ofType:@"plist"];

            [fileManager copyItemAtPath:bundlePath toPath:documentsPath error:&error];
        }

        [array writeToFile: documentsPath atomically:YES];

    }