Http://blog.csdn.net/xdrt81y/article/details/24331103Before reading this article, I first want to know the differences between copy and retain. If there are any errors, I hope you can give me more advice:
Differences between copy and retain:
Copy is to create a new object, retain is to create a pointer, reference object count plus 1. The copy attribute indicates that the content of the two objects is the same. The new object retain is 1, which is irrelevant to the reference count of the old object. The old object does not change. Copy reduces the context dependency of objects.
The retain attribute indicates that the address of the two objects is the same (create a pointer and copy the pointer), and the content is of course the same. The retain value of this object is + 1, that is, retain is the pointer copy, and copy is the content copy.
Of course, not all objects in IOS support copy and mutablecopy. classes that comply with the nscopying protocol can send copy messages. classes that comply with the nsmutablecopying protocol can send mutablecopy messages. If a copy or mutablecopy message is sent without complying with the appeal protocol, an exception occurs. However, the default IOS class does not comply with these two protocols. To customize copy, you must comply with nscopying and implement copywithzone: method. to customize mutablecopy, you must comply with nsmutablecopying and implement mutablecopywithzone: method.
First, we need to have the following premise:
[Array addobject: OBJ];
In this way, the reference count of OBJ will increase by 1. If you use remove, the reference count of OBJ will be reduced by one.
IOS processes the set memory in this way.
Assume that obj is only owned by array:
Id temp = [array objectatindex: 0];
[Array removeobjectatindex: 0];
If you want to use temp again, an error will occur because OBJ has been released.
(If you use nsstring for testing, note that @ "ABC" is a constant :-))
Because the collection class is often used to pass values in the program, a simple retain may not be enough. You need to copy the set content, that is, a deep copy.
Next we will discuss it.
IOS provides the copy and mutablecopy methods. As the name suggests, COPY Copies an imutable object, while mutablecopy copies a mutable object. The following are examples.
1. Non-container objects of the system
This refers to objects such as nsstring and nsnumber.
Nsstring * string = @ "origion ";
Nsstring * stringcopy = [String Copy];
Nsmutablestring * stringmcopy = [String mutablecopy];
[Stringmcopy appendstring :@"!! "];
Check the memory. We can find that string and stringcopy point to the same memory area (also called Apple's weak reference). In this case, the reference count of stringcopy is the same as that of string. Stringmcopy is what we call true replication. The system allocates new memory for it, but the Pointer Points to the same string as the string.
Let's look at the following example:
Nsmutablestring * string = [nsmutablestring stringwithstring: @ "origion"];
Nsstring * stringcopy = [String Copy];
Nsmutablestring * mstringcopy = [String Copy];
Nsmutablestring * stringmcopy = [String mutablecopy];
[Mstringcopy appendstring: @ "mm"]; // Error
[String appendstring: @ "origion! "];
[Stringmcopy appendstring :@"!! "];
The memory allocated by the above four nsstring objects is different. However, mstringcopy is actually an imutable object. Therefore, an error is returned.
For non-container objects in the system, we can think that for an immutable object, copy is Pointer copy (Shortest copy) and mutablecopy is object copy (deep copy ). If you copy a mutable object, it is a deep copy, but the object returned by copy is immutable.
2. system container objects
Nsarray, nsdictionary, etc. For the container class itself, the conclusions discussed above are also applicable. We need to discuss the changes of objects in the container after replication.
// Copy returns an immutable object. mutablecopy returns a mutable object.
Nsarray * array1 = [nsarray arraywithobjects: @ "A", @ "B", @ "C", nil];
Nsarray * arraycopy1 = [array1 copy];
// Arraycopy1 is the same nsarray object as array (pointing to the same object). The elements in array also point to the same pointer.
Nslog (@ "array1 retain count: % d", [array1 retaincount]);
Nslog (@ "array1 retain count: % d", [arraycopy1 retaincount]);
Nsmutablearray * marraycopy1 = [array1 mutablecopy];
// Marraycopy1 is a mutable copy of array1, pointing to an object different from that of array1, but the element and the element in array1 point to the same object. Marraycopy1 can also modify its own object
[Marraycopy1 addobject: @ "de"];
[Marraycopy1 removeobjectatindex: 0];
Array1 and arraycopy1 are pointer replication, while marraycopy1 is object replication. marraycopy1 can also change the elements in the period: delete or add. But note that all elements in the container are pointer copies.
Next we will use another example to test it.
Nsarray * marray1 = [nsarray arraywithobjects: [nsmutablestring stringwithstring: @ "A"], @ "B", @ "C", nil];
Nsarray * marraycopy2 = [marray1 copy];
Nslog (@ "marray1 retain count: % d", [marray1 retaincount]);
Nsmutablearray * marraymcopy1 = [marray1 mutablecopy];
Nslog (@ "marray1 retain count: % d", [marray1 retaincount]);
// Marraycopy2, marraymcopy1 and marray1 point to different objects, but the elements are the same object-the same pointer
// Perform a test
Nsmutablestring * teststring = [marray1 objectatindex: 0];
// Teststring = @ "1A1"; // This will change the pointer of teststring. In fact, the temporary object @ "1A1" is assigned to teststring.
[Teststring appendstring: @ "tail"]; // the first element of the preceding three arrays is changed.
It can be seen that the element object of a container is always pointer replication. If you want to copy element objects as objects, You need to perform deep copy. Http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Collections/Articles/Copying.html
Nsarray * array = [nsarray arraywithobjects: [nsmutablestring stringwithstring: @ "first"], [nsstringstringwithstring: @ "B"], @ "C", nil];
Nsarray * deepcopyarray = [[nsarray alloc] initwitharray: array copyitems: Yes];
Nsarray * truedeepcopyarray = [nskeyedunarchiver unarchiveobjectwithdata:
[Nskeyedarchiver archiveddatawithrootobject: array];
Truedeepcopyarray is a full-meaning deep copy, while deepcopyarray is not. For immutable elements in deepcopyarray, it is still a pointer copy. Or we can implement the deep COPY method by ourselves. If a certain element of the container is unchangeable, the object cannot be changed after you copy it. Therefore, you only need to copy the pointer. Pointer replication is sufficient unless you assign a value to the elements in the container. For example, after [[array objectatindex: 0] appendstring: @ "SD"], other objects in the container will not be affected. [[Array objectatindex: 1] and [[deepcopyarray objectatindex: 0] although they point to the same memory, we cannot modify it because it cannot be changed. Therefore, pointer replication is sufficient. Therefore, this is not entirely a deep copy, but Apple's official documentation lists it as deep copy and adds a description of the relationship between copy and mutablity, therefore, I would like to explain it here (I have some questions here and I am very grateful for your understanding ).
Or we can implement the deep COPY method by ourselves (omitted ).
3. Custom object
If the object is defined by us, we need to implement nscopying and nsmutablecopying so that we can call copy and mutablecopy. For example:
@ Interface myobj: nsobject <nscopying, nsmutablecopying>
{
Nsmutablestring * Name;
Nsstring * imutablestr;
Int age;
}
@ Property (nonatomic, retain) nsmutablestring * Name;
@ Property (nonatomic, retain) nsstring * imutablestr;
@ Property (nonatomic) int age;
@ End
@ Implementation myobj
@ Synthesize name;
@ Synthesize age;
@ Synthesize imutablestr;
-(ID) Init
{
If (Self = [Super init])
{
Self. Name = [[nsmutablestring alloc] init];
Self. imutablestr = [[nsstring alloc] init];
Age =-1;
}
Return self;
}
-(Void) dealloc
{
[Name Release];
[Imutablestr release];
[Super dealloc];
}
-(ID) copywithzone :( nszone *) Zone
{
Myobj * Copy = [[self class] allocwithzone: Zone] init];
Copy-> name = [Name copy];
Copy-> imutablestr = [imutablestr copy];
// Copy-> name = [name copywithzone: Zone];
// Copy-> imutablestr = [name copywithzone: Zone]; //
Copy-> age = age;
Return copy;
}
-(ID) mutablecopywithzone :( nszone *) Zone
{
Myobj * Copy = nscopyobject (self, 0, zone );
Copy-> name = [self. Name mutablecopy];
Copy-> age = age;
Return copy;
}
Retain copy (light copy) mutablecopy (deep copy)