将数据写入Plist

时间:2012-10-04 05:37:14

标签: iphone nsmutablearray plist

我有plist,其中包含一个字典dict,而在dict中我有3个数组用于键A,B,C。现在我想在特定情况下添加A,B和C中的项目。为此,我这样做......

    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *documentsDirectory = [path objectAtIndex:0];
    NSString *plistPath = [documentsDirectory stringByAppendingString:@"MovingChecklist.plist"];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    if (![fileManager fileExistsAtPath: plistPath]) 
    {
        NSString *bundle = [[NSBundle mainBundle] pathForResource:@"MovingChecklist" ofType:@"plist"];
        [fileManager copyItemAtPath:bundle toPath: plistPath error:&error];
    }
    NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
    NSArray *array1 = [dict objectForKey:@"A"];
    NSArray *array2 = [dict objectForKey:@"B"];
    NSArray *array3 = [dict objectForKey:@"C"];

    NSMutableArray *myArray;
    if (currentMovingList == KMovingListTypeA) {
        myArray = [NSMutableArray arrayWithArray:array1];
        [myArray addObject:nameTextView.text];
        [dict setValue:myArray forKey:@"A"];

    }
    if (currentMovingList == KMovingListTypeB) {
        myArray = [NSMutableArray arrayWithArray:array2];
        [myArray addObject:nameTextView.text];
        [dict setValue:myArray forKey:@"B"];
    }
    if (currentMovingList == KMovingListTypeC) {
        myArray = [NSMutableArray arrayWithArray:array3];
        [myArray addObject:nameTextView.text];
        [dict setValue:myArray forKey:@"C"];
    }
[dict writeToFile:plistPath atomically: YES];

但是plist并没有改变。建议我做错了什么。

1 个答案:

答案 0 :(得分:1)

我认为你应该把它设置为可变的

NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
相关问题