我有一个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并没有改变。告诉我我做错了什么。
发布于 2012-10-04 13:51:03
我认为您应该将它设置为可变的
NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];https://stackoverflow.com/questions/12720884
复制相似问题