0

我试图阅读很多问题/教程,但我找不到答案!我正在尝试从 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];

        }

其中数组是

@interface DetailViewController (){
    NSMutableArray* array;
}

但是当我尝试保存时应用程序崩溃(我之前写的操作连接到一个按钮)给出了这个错误:

*由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“无法使用 nil 模型创建 NSPersistentStoreCoordinator”

我认为编写的代码不正确,因为我从不输入要保存的密钥(如@“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];
}

怎么了?

4

1 回答 1

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];

    }
于 2013-10-18T11:06:52.557 回答