[OC] Object Copy


foreword

The NSObject class provides the copy and mutableCopy methods, through which a copy of an existing object can be copied

One, copy and mutableCopy method

mutable: mutable
The copy method is used to make a copy of an object. In general, copy always returns an unmodifiable copy of the object, even if the object itself is modifiable . For example, if we call the copy method of NSMutableString ( a modifiable string ), it will also return an unmodifiable string object.
The mutableCopy method is used to copy a mutable copy of an object. In general, the mutableCopy method always returns a modifiable copy of the object,even if the copied object itself is immutable, the copy copied by calling the mutableCopy method is also modifiable.
For example, if we call the mutableCopy method of NSString, an NSMutableString object will be returned.
In any case, copy and mutableCopy always return a copy of the original object. When the program modifies the assigned copy, the object will not be affected. Here
insert image description here
we must pay attention to the definition of NSRange. Here is its source code:
insert image description here
Output:
insert image description here
The above program can see that even our original object is immutableReplicated copies are mutable, the copy we get must also be mutable, the immutability of the copy is related to the way it is copied, and has nothing to do with the object it is copied from .

Two, NSCopying and NSMutableCopy protocol

We just called the copy and mutablecopy methods in the class defined by OC. These two methods are really convenient for copying objects. So can our custom classes call copy and mutablecopy?
The answer is no, so I won’t go into details here, just post the code in the book.
insert image description here
insert image description here
insert image description here
When we call the object's copy method to copy itself, our program's underlyingActually call the copyWithXxxx: methodto complete the actual replication work. What copy returns is actually the return value of the copyWithXxxx: method. The same goes for mutableCopyWithZone.
But we must remember that the method signature of the method we rewrite must be copyWithZone, which is a fixed format

Next we give the code:
the interface part of the protocol:
insert image description here
the interface part of the custom class:
insert image description here
the implementation part:
insert image description here
the main function part:
insert image description here
the output result:
insert image description here
here we can see that our program copies a copy, assigns the copied copy to the b variable, and then reassigns b.
We see that although we call the copy method, the string we copied can still be modified. And it can be seen that a is not affected when the program modifies b.

insert image description here
The main meaning of our passage is that in our NSString class, the function of ensuring NSString immutability has been implemented, but our custom class has not implemented it, so we can also modify the copied copy when we call the copy method.

3. Shallow copy and deep copy

Let's look at the following program:

The implementation part of the class:
insert image description here

Function part:
insert image description here

insert image description here

When we modify d2, our program also changes the member variable of d1 object at the same time, but the member variable d1 of int type does not change accordingly.
insert image description hereinsert image description here
From the above figure, we can see that although our program creates two objects, the member variables of the pointer variable type in the two objects actually point to the same object (the name of this object is Wangcai). So when we modify the two objects we copied and pasted, the properties of the two objects will change accordingly.
From this we come to the definition of shallow copy:When the member variable of an object is a pointer variable, if the program just copies the address of the pointer instead of actually copying the object pointed to by the pointer , then this method is called shallow copying.
In addition, what we need to know is that the name in our figure is just a pointer variable ,Only the address of the string is stored in this variable, not the string itself. This needs to be memorized.

Then we know that the member variables of our shallow copy party object cannot be modified separately when they have pointer variables, so here comes our deep copy: the object itself can be copied,and will "recursively" copy each instance variable of pointer type.
To achieve deep copy, we only need to recursively copy the member variables of our pointer type. In this way ,
insert image description here
we have successfully modified the pointer variable of the object we copied.
insert image description here
Generally speaking, the implementation of deep copy is much more difficult, especially when the object contains a large number of instance variables of pointer type.
Generally speaking, most of the classes in our foundation framework only implement shallow copying .

Fourth, the copy option of the setter method

When we wrote the program earlier, in fact, the author has never understood the meaning of calling copy in property, but I learned a little bit about it.
First of all, the copy indicator is to specify that when the program calls the setter method to copy, it actually assigns a copy of the incoming parameter to the member variable of our actual program .
We use property when defining the interface, so we will not give the code here, just look at the main function part: we
insert image description here
can see that there is nothing wrong with the assignment operation of name, == and it is indeed assigned to a variable string type. == But something went wrong when we modified this member variable.
This is because we used the copy indicator when we defined the name attribute. When we use the dot syntax to assign a value , == actually calls the corresponding setter method, and our program actually uses a copy of the parameter to copy the name instance variable.
//This step actually refers to
a.name = [NSMutableString stringWithString:@"二狗"];
the code of our setName: method is as follows:
insert image description here
because our copy copies an immutable copy by default, even if the program passes in NSMutableString, but because we get an immutable copy when we use the dot syntax, the instance variable of the name of the object is still an immutable string, so it cannot be modified

Guess you like

Origin blog.csdn.net/weixin_72437555/article/details/130412773