Deep copy and shallow copy

Copy is a shallow copy of data, and mutableCopy is a deep copy of data.

NSArray

NSArray is ubiquitous in development, so let’s take a look at the shallow copy and full copy effects.

As can be seen from the above code and the printed results, NSArray is copied in different ways, and the final conclusion is as follows:

Copy is a shallow copy, and only a pointer copy is made, so there is only one copy of NSArray data in memory, with two Pointers pointing to it

NSMutableArray

NSMutableArray is the most commonly used in development, especially when you have tabular data, so take a look at how NSMutableArray works in different ways.

As can be seen from the above code and the printed results, NSMutableArray is copied in different ways, and finally comes to the following conclusions:

1, Copy is a deep copy, but the mutable array is copied into an immutable array. There are two copies of data in memory, one is the NSMutableArray type of data, the other is the NSArray type of data. In addition, the copied data is of NSMutableArray type. There are two copies in memory, and each one has a pointer to it

NSDictionary

NSDictionary dictionary data is ubiquitous in development and features key-value access. So what happens when YOU copy NSDictionary in different ways?

As can be seen from the above codes and the printed results, NSDictionary is copied in different ways, and the final conclusions are as follows:

Nsdictionary-type data is a shallow copy, and only a pointer copy is made. There is only one copy of nsDictionary-type data in memory, and there are two Pointers to it. Each has a pointer to the data in memory

NSMutableDictionary

NSMutableDictionary is most commonly used in development, especially with tabular data, so take a look at how NSMutableDictionary is copied in different ways.

As can be seen from the above code and the printed results, NSMutableDictionary copies in different ways and finally reaches the following conclusions:

1. Copy is a deep copy, but the mutable array becomes an immutable dictionary after being copied. The first copy is the NSMutableDictionary. Nsmutabledictionarycopy (nSMutableDictionarycopy, nsmutableDictionarycopy, nsmutableDictionarycopy, NSMutableDictionary

LGPerson

LGPerson also needs to copy objects in development, but do we need deep or shallow copies? Save this question for now, and let’s see the effect:

NSCopying or NSMutableCopy must be implemented if the object is to be copied. Both of these protocols require only one method: – (nonnull id)copyWithZone:(nullable NSZone *)zone and – (nonnull id)mutableCopyWithZone:(nullable NSZone *)zone. Let’s look at the implementation:

@implementation LGPerson

- (nonnull id)copyWithZone:(nullable NSZone *)zone {
    LGPerson *p = [[[self class] allocWithZone:zone] init];
    return p;
}

- (nonnull id)mutableCopyWithZone:(nullable NSZone *)zone {
    LGPerson *p = [[LGPerson alloc] init];
    return p;
}

@end
Copy the code

There are no write attributes, but the object is a deep copy with or without attributes. We can verify this by printing above.

conclusion

Through the above verification, we can get the following conclusions:

1. For non-container objects such as NSString, copy is a shallow copy and mutableCopy is a deep copy.

2. For a non-container object, if it is a mutable object, such as NSMutableNSString, copy is a deep copy, but the copied object is immutable. MutableCopy is a deep copy and its type is also mutable.

3. Copy is a shallow copy and mutableCopy is a deep copy of an immutable container object such as NSArray.

4, for the system container class object, if it is a mutable object, such as: NSMutableArray, copy of the object, copy is a deep copy, but the copied object is immutable, mutableCopy is a deep copy, the type is also mutable.

5. For user-defined objects, both copy and mutableCopy are deep copies