Agreement
NSCopying protocol is the basic protocol in OC.
/*************** Basic protocols ***************/
@protocol NSCopying
- (id)copyWithZone:(nullable NSZone *)zone;
@end
Follow this protocol in the CCYBook class and implement the method.
- (id)copyWithZone:(NSZone *)zone{
CCYBook *book;
book = [[[self class] allocWithZone:zone] init];
return book;
}
If this protocol is not implemented, the object's copy method cannot be called. Because the copy method calls the copyWithZone method.
Shallow copy and deep copy
Look at the code below
CCYBook *book = [[CCYBook alloc] init];
//CCYBook *abook = [book copy];
NSLog(@"book = %p", book);
NSArray *array = [NSArray arrayWithObjects:book, nil];
NSMutableArray *mutArray = [NSMutableArray arrayWithObjects:book, nil];
NSLog(@"array[0] = %p", array[0]);
NSLog(@"mutArray[0] = %p", mutArray[0]);
id arraycopy = [array copy];
id arraymutableCopy = [array mutableCopy];
id mutArraycopy = [mutArray copy];
id mutArraymutableCopy = [mutArray mutableCopy];
NSLog(@"**************************");
NSLog(@"arraycopy = %@", [arraycopy class]);
NSLog(@"arraymutableCopy = %@", [arraymutableCopy class]);
NSLog(@"mutArraycopy = %@", [mutArraycopy class]);
NSLog(@"mutArraymutableCopy = %@", [mutArraymutableCopy class]);
NSLog(@"**************************");
NSLog(@"array[0] = %p", array[0]);
NSLog(@"mutArray[0] = %p", mutArray[0]);
NSLog(@"arraycopy[0] = %p", arraycopy[0]);
NSLog(@"arraymutableCopy[0] = %p", arraymutableCopy[0]);
NSLog(@"mutArraycopy[0] = %p", mutArraycopy[0]);
NSLog(@"mutArraymutableCopy[0] = %p", mutArraymutableCopy[0]);
NSLog(@"**************************");
NSArray *depthCopyArray = [[NSArray alloc] initWithArray:mutArray copyItems:true];
NSLog(@"depthCopeArray[0] = %p", depthCopyArray);
Create a Book class object, add it to the array, send a copy message to the array. The input results are as follows:
2018-08-16 21:49:39.188019+0800 demo0816[1735:91988] book = 0x100601fa0
2018-08-16 21:49:39.188368+0800 demo0816[1735:91988] array[0] = 0x100601fa0
2018-08-16 21:49:39.188416+0800 demo0816[1735:91988] mutArray[0] = 0x100601fa0
2018-08-16 21:49:39.188488+0800 demo0816[1735:91988] **************************
2018-08-16 21:49:39.188518+0800 demo0816[1735:91988] arraycopy = __NSArrayI
2018-08-16 21:49:39.188544+0800 demo0816[1735:91988] arraymutableCopy = __NSArrayM
2018-08-16 21:49:39.188569+0800 demo0816[1735:91988] mutArraycopy = __NSSingleObjectArrayI
2018-08-16 21:49:39.188582+0800 demo0816[1735:91988] mutArraymutableCopy = __NSArrayM
2018-08-16 21:49:39.188591+0800 demo0816[1735:91988] **************************
2018-08-16 21:49:39.188603+0800 demo0816[1735:91988] array[0] = 0x100601fa0
2018-08-16 21:49:39.188614+0800 demo0816[1735:91988] mutArray[0] = 0x100601fa0
2018-08-16 21:49:39.188625+0800 demo0816[1735:91988] arraycopy[0] = 0x100601fa0
2018-08-16 21:49:39.188637+0800 demo0816[1735:91988] arraymutableCopy[0] = 0x100601fa0
2018-08-16 21:49:39.188647+0800 demo0816[1735:91988] mutArraycopy[0] = 0x100601fa0
2018-08-16 21:49:39.192014+0800 demo0816[1735:91988] mutArraymutableCopy[0] = 0x100601fa0
2018-08-16 21:49:39.192079+0800 demo0816[1735:91988] **************************
2018-08-16 21:49:39.192139+0800 demo0816[1735:91988] depthCopeArray[0] = 0x100604cf0
Only the last array created calls the object's copyWithZone method.