Small knowledge, big challenge! This paper is participating in theEssentials for programmers”Creative activities

This paper also participates inProject DigginTo win the creative gift package and challenge the creative incentive money

See the essence through the problem!!

It’s often confusing to know the difference between copy and mutableCopy, and whether it’s a deep copy or a shallow copy. In fact, as long as you remember the essence and know the purpose of copying, all problems can be solved.

Purpose of copying

The purpose of copying: to create a copy of an object that does not affect the original object.

The source object has been modified without affecting the replica object.

The replica object is modified without affecting the source object.

It’s like we have a readme.md file on our computer. We copy the readme.md file to get a copy of the readme.md file. No matter which file we modify, it does not affect the other file.

Shallow copy, deep copy

If you use copy, which is an immutable copy, you’ll get an immutable copy, and if you use mutableCopy, you’ll get a mutableCopy. We extended from copy to shallow copy, deep copy.

Shallow copy: a copy of a pointer that points to the same object without creating a new object.

Deep copy: Copy content, creating new objects with Pointers to different objects.

Immutable object copy operation

void testString1() { NSString *string1 = [[NSString alloc] initWithFormat:@"test---String"]; NSString *string2 = [string1 copy]; NSMutableString *string3 = [string1 mutableCopy]; NSLog(@"%@ %@ %@", string1, string2, string3); NSLog(@"%p %p %p", string1, string2, string3); // Printed result: test-- String test-- String test-- String // Printed result: 0x10063B2B0 0x10063B2B0 0x10063AD80}Copy the code
The copy operation

Immutable object (NSString *) string1, which calls copy and does not produce a new object, is a shallow copy.

Because string1 is immutable, it cannot be modified.

After copy, you get immutable objects. Since both objects are immutable, you can simply point to the same object and save memory.

Therefore, immutable object copy operation is a shallow copy.

MutableCopy operation

Immutable object (NSString *) string1, if you call mutableCopy, it will generate a new object, a deep copy.

Because string1 is immutable, it cannot be modified.

So mutableCopy then gets a mutable object, and since it’s a mutable object, it can be modified. And after modification can not affect the original object, this is the purpose of copy. So mutableCopy creates a new object, so no matter how you modify it, it doesn’t affect the source object.

So immutable object mutableCopy is a deep copy.

Mutable object copy operation

void testString2() { NSMutableString *mutableString1 = [[NSMutableString alloc] initWithFormat:@"test-mutableString"]; NSString *mutableString2 = [mutableString1 copy]; NSMutableString *mutableString3 = [mutableString1 mutableCopy]; // Deep copy NSLog(@"%@ %@ %@", mutableString1, mutableString2, mutableString3); NSLog(@"%p %p %p", mutableString1, mutableString2, mutableString3); // Print a result: test-mutableString test-mutableString test-mutableString 0x1006114B0 0x100611500 0x100605d60}Copy the code
The copy operation

Mutable object (NSMutableString *) mutableString1, calling copy produces a new object, a deep copy.

Because mutableString1 is a mutable object, it can be modified.

Copy after the object is also immutable, can not be modified. So one can be modified, one can’t be modified, and in order for them not to interfere with each other, new objects must be created.

So mutable object copy is a deep copy.

MutableCopy operation

Mutable object (NSMutableString *) mutableString1, which calls the mutableCopy method, produces a new object, which is a deep copy.

Because mutableString1 is an immutable object, it can be modified.

So mutableCopy then gets a mutable object, and since it’s a mutable object, it can be modified.

Both of them can be modified, and in order for them not to interfere with each other, new objects must be created.

So mutable object mutableCopy operation, deep copy.

conclusion

  • Copy an immutable object is a shallow copy

  • Immutable object mutableCopy operation, is a deep copy

  • Mutable object copy is a deep copy operation

  • Mutable object mutableCopy operation, is a deep copy