Reputation: 4235
I make some test in Objective-C and i've got question about copy
method. What is difference between:
id object2 = [object copy];
and:
id object2 = object;
?
Is difference visible when object = nil
after this annotation? Then in first case object2
is equal to object
but in second case object2
is equal to nil
? When to use copy
method correctly?
Thank you for explanation.
Upvotes: 0
Views: 69
Reputation: 21966
The difference is in the fact the if you copy the object you will have an independent copy, and if you alter that object, the original object will not be affected.
For example you have a NSNumberFormatter:
NSNumberFormatter* f1= [NSNumberFormatter new];
f1.numberStyle= NSNUmberFormatterDecimalStyle;
NSNumberFormatter* f2= [f1 copy];
f2.numberStyle= NSNUmberFormatterScientificStyle;
In this case f1 isn't affected by the change.
But if you make a simple assignment:
NSNumberFormatter* f1= [NSNumberFormatter new];
f1.numberStyle= NSNUmberFormatterDecimalStyle;
NSNumberFormatter* f2= f1;
f2.numberStyle= NSNUmberFormatterScientificStyle;
Then also f1 is affected by the change.
If you use immutable objects you shouldn't care about this difference.
Upvotes: 2
Reputation:
Using the standard object2 = object is a referencing or pointing assignment. It means that both object and object2 should point to the same region of memory. Any changes applied to either object should effect them both simultaneously (or more correctly they both read and write from the exact same memory location).
When you use copy you are creating a new section in memory and populating it with identical values as the object. In this scenario you have 2 separate areas of memory that start with the same values. As you edit object you are editing the values in memory for only that variable name. When you edit the values of object 2 you are editing only the values in memory only for object 2.
When you mean for both variable names to point to the exact same area of memory then you definitely want to use the referencing method to limit memory usage and overhead. If these two objects are meant to be independent of each other you want to create and maintain a copy.
Upvotes: 2
Reputation:
The first approach copies an object (if it conforms to the <NSCopying>
protocol) - i. e. it creates a new instance of the object that is logically equivalent to the old instance. The two objects will be at different places in memory.
+----------+ +---------------------------------------------+
| Object 1 | | Copy of object 1, with identical properties |
+----------+ +---------------------------------------------+
^ ^
| |
"object" "object2"
The second line only assigns the pointer to object
to the variable object2
- it doesn't create a new instance, the pointers will point to the exact same object:
+--------+
| Object |
+--------+
^ ^
| +--------------+
"object" "object2"
Is difference visible when object = nil?
No, since then both variables will be nil
(messaging nil
in Obejctive-C always yields nil
or 0).
When to use copy method correctly?
Whenever you need to duplicate an existing object, for example when you want to modify some properties on it without affecting the original object.
Upvotes: 3