In this code from an array i am selecting a dictionary,modifying it and saving back in another array .but i dont know why at the second last line of this code ie where i am inserting the dict it is crashing (message sent to deallocated instance).how can i fix this
NSArray *array=[NSArray arrayWithContentsOfFile:plistPath];
NSLog(@"array before %@",array);
NSMutableArray *tempArray=[[NSMutableArray alloc]init];
tempArray=(NSMutableArray*)array;
NSMutableDictionary *dictToBeChanged=[[NSMutableDictionary alloc]init];
dictToBeChanged=[tempArray objectAtIndex:indexPath.row];
[dictToBeChanged setObject:[NSNumber numberWithBool:YES] forKey:@"isPaid"];
[tempArray removeObjectAtIndex:indexPath.row];
[tempArray insertObject:dictToBeChanged atIndex:indexPath.row];
NSLog(@"array after %@",tempArray);
When you assign
arraytotempArrayyou don’t make it mutable just because you cast it.It’s an
NSArray, so you can’t add/remove its objects.Also, there are a few unneeded initializations (of tempArray and dictToBeChanged) since you’re overwriting those variables with something else right after initializing (thus creating leaks).
What you need is probably something like this:
Note that this code doesn’t do any validations on the contents of your plist.