My goal is to be able to update objects in NSUserDefaults.
This is my approach, and my concern is that when I replace the arrays using removeObjectForKey, is there any memory issue than can be created. The objects that were in the array are saved in objectArray by means of initWithArray:. oldSavedArray is the array with the previous contents of NSUserDefaults.
objectArray = [[NSMutableArray alloc] initWithArray:oldSavedArray];
if ([objectArray containsObject:myObject]){
// remove the current object
[objectArray removeObject:_currentFood];
// reset our new array of objects
[_defaults removeObjectForKey:@"mySavedData"];
[_defaults setObject:[NSKeyedArchiver archivedDataWithRootObject:objectArray] forKey:@"mySavedData"];
}
else{
// add the current object
[objectArray addObject:_currentFood];
// reset our new array of favorite food
[_defaults removeObjectForKey:@"mySavedData"];
[_defaults setObject:[NSKeyedArchiver archivedDataWithRootObject:objectArray] forKey:@"mySaveData"];
}
To answer your question
My answer is No. But that step is not necessary.
is enough.