1

我有一个包含字典的 plist,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 没有改变。建议我做错了什么。

4

1 回答 1

1

我认为您应该将其设置为可变的

NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
于 2012-10-04T05:51:03.437 回答