The copy of the object is to copy an object as a copy, and he will open up a new memory (heap memory) to store the replica object, just as the file is copied, that is, the source object and the replica object are two different areas of memory. object to have replication function, must implement the <NSCopying> protocol or <NSMutableCopying> protocol, commonly used replicable objects are: NSNumber, NSString, nsmutablestring, Nsarray, Nsmutablearray, Nsdictionary, nsmutabledictionary
Copy: The copy of the resulting object is immutable
Mutablecopy: The copy of the resulting object is mutable
Shallow copy and deep copy
Shallow copy values copy the object itself, the attributes in the object, the contained objects do not replicate
A deep copy copies the object itself, and the object's properties copy a
A class in the foundation that supports replication, by default shallow copy
Custom copies of objects
An object has a copy attribute, which implements the Nscopying,nsmutablecopying protocol to implement the Copywithzone: Method or Mutablecopywithzone: Method of the Protocol.
Shallow copy implementation
[CPP]View Plaincopyprint?
- -(ID) Copywithzone: (Nszone *) zone{
- Person *person = [[[Self class]allocwithzone:zone]init];
- P.name = _name;
- P.age = _age;
- return &NBSP;PERSON;&NBSP;&NBSP;
- }
implementation of deep copy
[CPP]View Plaincopyprint?
- -( void
- person *person = [[[self class]allocwithzone:zone]init];
- Person.name = [_name copy];
- Person.age = [_age copy];
- return &NBSP;PERSON;&NBSP;&NBSP;
- }
the relationship between a deep copy and a retain
The relationship between copy, Mutablecopy, and retain
The replicable object in the foundation, when we copy an immutable object, it works fairly well with retain (Cocoa memory optimizer)
When we use Mutablecopy, the copy is mutable regardless of whether the source object is mutable or not.
When we copy a mutable object, the copy is not mutable
Copy, mutablecopy, shallow copy, deep copy of objects in Objective-c