That's because both item
and editedToDoItem
point to the same object — and the same exact location in memory. When you write item = toDoItem
, what you're really doing is saving the pointer to toDoItem
inside the variable item
. It's a reference to the original object, not a copy, because Objective-C object variables are pointers to the objects in memory.
Consider the following code:
NSMutableString *string = [@"Hello, world!" mutableCopy];
NSMutableString *a = string;
NSMutableString *b = string;
// string, a, and b point to the same exact object.
[string appendString:@" Hi again!"];
NSLog(@"%@", a); // => "Hello, world! Hi again!"
[b appendString:@" Whoa!"];
NSLog(@"%@", a); // => "Hello, world! Hi again! Whoa!"
When you fetch the item from your array, then store it twice in the editing controller, you're just passing around references to the same object, which, when edited, will reflect on all other references that you have — because they point to the same location in memory.
If you really want several different copies of the object (so that editing one doesn't affect the others), you have to actually copy the objects by making them conform to the NSCopying Protocol and using [item copy]
.