Reputation: 1692
I have plist which contains a dictionary say dict
and in dict I have 3 arrays for keys A,B,C. Now I want to add an item in A,B and C under particular situation. For this my doing like this...
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];
But the plist is not changing. Suggest me what mistake m doing.
Upvotes: 1
Views: 150
Reputation: 2807
I think you should set this as mutable
NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
Upvotes: 1