OC Details, details of Public gun battles
Overview
- Copy: copy an object with the same content as the source object
To copy data, you must comply with the following two Protocols:
- NSCopying
- NSMutableCopying
Types of objects returned for copying
- Variable, the object returned by the mutableCopy message
- Immutable. The object returned by the copy message
Copy type
- The shortest copy only copies a pointer to the source object. No object is created and no memory is allocated.
- Deep copy: Copies the source object, creates a new object, and allocates memory.
Note:
- The system processes the memory of the container class object and the non-container Class Object differently. That is, when an object not strongly referenced by other objects is removed from the container, the object will be destroyed.
Copy and Retain
Copy
- Is to create a new object, copy the content
- Copy indicates that the content of the two objects is the same, and the reference count of the new object is 1.
- It is irrelevant to the reference count of the old object, so the object does not change.
- Copy reduces the context
Retain
- Creates a pointer and copies the pointer.
- The object address is the same, and the content is the same.
- Object reference count + 1
Copying behavior of different objects
--- Restore content end ---