Copy & Mutablecopy
Copy & Mutablecopy
Use the copy or Mutablecopy method to create a copy of an object
Copy
Need to implement Nscoppying protocol
Immutable replicas are created (such as NSString, Nsarray, Nsdictionary)
Mutablecopy
Need to implement the Nsmutablecopying protocol first
A mutable copy is created (such as nsmutablestring, Nsmutablearray, Nsmutabledictionary)
The purpose of copy is to create a copy, while modifying the original object and copy without interfering with each other
Deep Copy & Sneak copy
Deep copy:
Content copy, the source object and the copy point to a different two objects
The source object reference counter is unchanged, and the replica counter is set to 1
Shallow copy:
Pointer copy, source object and copy point to the same object
Object reference counter +1 is actually equivalent to doing a retain operation
Only immutable objects Create immutable replicas (copy) is shallow copy, others are deep copy
Copywithzone of Custom objects
-(ID) Copywithzone: (Nszone *) zone
{
Person *p = [[[Self class] allocwithzone:zone] init];
P.name = Self.name;
P.age = Self.age;
return p;
}
About NSNumber & int Nsinteger
Int is in the stack area, high efficiency, fast, can be put in the method of internal use
NSNumber is the object, in the heap, with the function of numerical conversion, the efficiency is poor, convenient KVC assignment
Q & A
Copy & Mutablecopy