A. As mentioned in the comment to your Q, there is no new object. You should really understand the difference between (Objective-C) object and object pointer. In my books I usually use code like below to explain that:
// Copy reference
NSMutableString *ref1 = [@"Amin" mutableCopy];
NSMutableString *ref2 = ref1;
[ref1 appendString:@" Negm"];
NSLog( @"%@ / %@", ref1, ref2 );
You will get
$ Amin Negm / Amin Negm
What? Both objects changed? No, there is only one object and two pointers to it. If you change the one object using one pointer it is the one changed object even you address it using the second pointer. I. e. this is the semantically difference between strong
and copy
for declared properties.
To have two objects, you have to explicitly copy the first object:
// Copy reference
NSMutableString *ref1 = [@"Amin" mutableCopy];
NSMutableString *ref2 = [ref1 mutableCopy];
[ref1 appendString:@" Negm"];
NSLog( @"%@ / %@", ref1, ref2 );
In this case the object ref1
points to is copied, so we have two objects, and then the reference to the new object is assigned to ref2
. In this case you have two different objects and you will get the expected result:
$ Amin Negm / Amin
B. Why should one assign the reference to a new var? Beside the reason mentioned in Logan's answer (which is not sufficient in this case, we are talking about saving, that means disk access and a method invocation does not change the performance in a relevant way), there is a different reason: Code readability
1: Using a second reference var has the advantage that the code is more compact. It is simply shorter and easier to overlook.
2: Using a second reference var has the advantage that you get a new label. Using var names like oldContext
and newContext
or backgroundContext
and mainContext
or $whatever makes it easy to the reader to follow the data flow in a complex algorithm.