In the development project, many times we want to create a copy of the object, to create a copy, the object's class must abide by the nscopying and nsmutablecopying protocol:
@protocol nscopying
-(ID) Copywithzone: (Nszone *) zone;
@end
@protocol nsmutablecopying
-(ID) Mutablecopywithzone: (Nszone *) zone;
@end
The classes of the General Foundation are subject to both protocols and can be copied directly.
Replication in a variety of situations
A: Non-container Class immutable object copy, copy does not produce a copy, only mutablecopy to produce a copy.
1. Copy of Immutable objects
Copy is a shallow copy that duplicates only the value of the object pointer, does not produce a copy, and only increases the object's reference count.
NSString * ns1 = [[NSString alloc] Initwithformat:@ "%@"@ "1234" ]; * copyns1 = [ns1 copy]; NSLog (@ "copy:%lu %lu", Ns1.retaincount,copyns1.retaincount);
2. Mutablecopy of Immutable objects
Mutablecopy produces a mutable object, which is a copy, which is a deep copy, but does not change the reference count of the original object.
NSString * ns1 = [[NSString alloc] initwithformat:@ "%@", @ "1234"];
nsmutablestring * mcns1 = [ns1 mutablecopy];
NSLog (@ "%lu %lu %lu", Ns1.retaincount,copyns1.retaincount, Mcns1.retaincount);
Second: A non-container class of variable object replication, whether copy or Mutablecopy will produce a new copy.
1. Copy of a Mutable object
Copy produces a new immutable object. (Produces a copy)
nsmutablestring * ms1 = [[nsmutablestring alloc] Initwithformat:@ "%@"@ " 4567" ]; NSString* copyms1 = [ms1 copy]; NSLog (@ "muablecopy:%lu %lu", Ms1.retaincount,copyms1.retaincount) ;
2. Mutablecopy of Variable objects
The mutablecopy produces a new mutable object. (Produces a copy)
nsmutablestring * ms1 = [[nsmutablestring alloc] initwithformat:@ "%@", @ "4567" ];
nsmutablestring * mcms1 = [Ms1 mutablecopy];
NSLog (@ "%lu %lu %lu", Ms1.retaincount,copyms1.retaincount, mcms1.retaincount);
Copy and mutablecopy of three non-mutable container classes (the replication of container classes is by default a shallow copy, that is , only the container class object itself is copied, not the objects in the container )
1.copy does not produce a new copy, only increases the reference count of the array itself, and the reference count of objects within the array does not increase.
Nsarray *arr1=[nsarray arraywithobjects:@1, @2, @3, @4*copyarray=[ ARR1 copy]; NSLog (@ "%p%p", Arr1,copyarray);
NSLog (@ "%lu%lu"longlong) copyarray.retaincount);
2.MutableCopy produces a copy, which is a mutable array, and the reference count of the original array itself does not increase, but the reference count of the objects within the array increases.
Nsarray *arr1=[nsarray arraywithobjects:@1, @2, @3, @4*copyarray=[ ARR1 Mutablecopy]; NSLog (@ "%p%p", Arr1,copyarray); NSLog (@ "%lu%lu"longlong) copyarray.retaincount)
Copy and mutablecopy of four variable container classes
1.copy produces a copy, which is an immutable array, and the reference count of the objects within the array increases.
2.MutableCopy also produces a copy, which is a mutable array, and the reference count within the array increases.
A summary of copy and mutablecopy:
For immutable objects, copy does not produce a new object, which acts as a retain and increases the reference count. Mutablecopy produces a new Mutable object, and the reference count of the original object is unchanged.
For mutable objects, copy and mutablecopy both produce replicas, that is, new objects, but copy is immutable and mutablecopy is a mutable copy.
A summary of the copy and Mutablecopy results:
The result of copy is immutable, and the result of mutablecopy is variable.
Copy and mutablecopy of iOS development