将数据保存到xcode中的plist

时间:2014-05-18 05:37:38

标签: objective-c plist

据我所知,您必须先从plist中检索数据,添加数据,然后将整个内容重写为plist文件。

但是我的代码不是追加,而是每次调用这个方法时都会刷新。

- (void) saveDataToPlist {
    // Retrieve path and filename of the plist file
    NSString *myColorsListFile = [self dataFilePath];

    NSMutableArray  *innerArray1;
    NSString *error;

    UserGivenColorHexString = HexTextField.text;
    NSMutableDictionary *rootObj = [NSMutableDictionary dictionaryWithCapacity:100];

    if ([[NSFileManager defaultManager] fileExistsAtPath:myColorsListFile]) {
        // File exists
        rootObj = [[NSMutableDictionary alloc] initWithContentsOfFile:myColorsListFile];


        [innerArray1 addObjectsFromArray:[NSMutableArray arrayWithContentsOfFile:myColorsListFile]];
        [innerArray1 addObject:UserGivenColorName];

    } else {
        // Create file
        rootObj = [[NSMutableDictionary alloc] init];

        innerArray1 = [NSMutableArray arrayWithObjects: UserGivenColorName, nil];
        [rootObj setObject:innerArray1 forKey:@"ColorName"];
    }

    id plist = [NSPropertyListSerialization dataFromPropertyList:(id)rootObj format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];

    [plist writeToFile:myColorsListFile atomically:YES];
}

1 个答案:

答案 0 :(得分:3)

似乎存在一系列问题:关键问题是,如果找到了plist,则不会设置innerArray1变量,因此您尝试添加该变量将是徒劳的。在使用plist的内容加载rootObj之后,您必须获取对位于键ColorName的数组的引用:

innerArray1 = [rootObj objectForKey:@"ColorName"];

然后代码就可以了。

与此直接相关,还有一些其他小问题:

  • 您正在使用myColorsListFile plist阅读字典,但您也尝试将此内容作为数组添加到innerArray1。后者使用myColorsListFile打击了我对这个plist的无效使用。我删除了该行。

  • 您似乎设置了UserGivenColorHexString,但随后保存UserGivenColorName(我也可能将其更改为本地变量以避免其他地方出现意外后果)。我假设您打算对这两个变量使用相同的变量。

  • 您还要实例化您随后丢弃的可变字典。

  • 我可能会建议遵守Cocoa Naming Conventions(用小写字母开始变量)。我还假设hexTextField确实属于财产。

  • 分离"文件是否存在可能会更强大一些"来自"的逻辑我找到了ColorName数组"逻辑。

  • 由于您正在捕获plist创建失败错误,因此如果遇到错误,也可以将其记录下来。

  • documentation告诉我们dataFromPropertyList现已过时:

      

    特别注意事项

         

    此方法已过时,很快就会弃用。请改用dataWithPropertyList:format:options:error:

所以,你最终会得到类似的东西:

- (void) saveDataToPlist {
    // Retrieve path and filename of the plist file
    NSString *myColorsListFile = [self dataFilePath];

    NSMutableArray  *innerArray1;
    NSError *error;

    NSString *userGivenColorHexString = HexTextField.text;
    NSMutableDictionary *rootObj;

    if ([[NSFileManager defaultManager] fileExistsAtPath:myColorsListFile]) {
        rootObj = [[NSMutableDictionary alloc] initWithContentsOfFile:myColorsListFile];
    } else {
        rootObj = [[NSMutableDictionary alloc] init];
    }

    innerArray1 = [rootObj objectForKey:@"ColorName"];

    if (innerArray1) {
        [innerArray1 addObject:userGivenColorHexString];
    } else {
        innerArray1 = [NSMutableArray arrayWithObjects: userGivenColorHexString, nil];
        [rootObj setObject:innerArray1 forKey:@"ColorName"];
    }

    id plist = [NSPropertyListSerialization dataWithPropertyList:(id)rootObj format:NSPropertyListXMLFormat_v1_0 options:0 error:&error];
    if (!plist) {
        NSLog(@"dataFromPropertyList error: %@", error);
    }

    [plist writeToFile:myColorsListFile atomically:YES];
}