淺拷貝就是拷貝后馏臭,并沒有進(jìn)行真正的復(fù)制舔庶,拷貝后的對(duì)象和原對(duì)象都指向同一塊內(nèi)存地址
深拷貝是真正的復(fù)制了一份白胀,復(fù)制的對(duì)象指向了新的內(nèi)存地址
淺拷貝就相當(dāng)于自己的影子,而深拷貝則是克隆的你
//注意
//copy 在拷貝不可變對(duì)象的時(shí)候?yàn)闇\拷貝谈秫,但是拷貝可變對(duì)象的時(shí)候?yàn)樯羁截?//mutablecopy 始終為深拷貝
copy
NSLog(@"-----------------copy-----------------");
//不可變對(duì)象
NSString *str = @"字符串";
NSString *copyStr = str.copy;
NSLog(@"\n不可變字符串");
NSLog(@"\nstr_p:%p,\ncopyStr_p:%p",str,copyStr);
// 不可變字符串
// str_p:0x1043b72a8,
// copyStr_p:0x1043b72a8
NSArray *array = [NSArray array];
NSArray *copyArray = array.copy;
NSLog(@"\n不可變數(shù)組");
NSLog(@"\narray_p:%p,\ncopyArray_p:%p",array,copyArray);
// 不可變數(shù)組
// array_p:0x6040000091f0,
// copyArray_p:0x6040000091f0
//copy在拷貝不可變對(duì)象的時(shí)候?yàn)闇\拷貝扒寄,淺拷貝的對(duì)象共用同一塊內(nèi)存地址
//可變對(duì)象
NSMutableString *mutablestr = [[NSMutableString alloc] initWithString:@"可變字符串"];
NSString *copyMutablestr = mutablestr.copy;
NSLog(@"\n可變字符串");
NSLog(@"\nmutablestr_p:%p,\ncopyMutablestr_p:%p",mutablestr,copyMutablestr);
// 可變字符串
// mutablestr_p:0x60000005a700,
// copyMutablestr_p:0x600000059c50
NSMutableArray *mutableArray = [NSMutableArray arrayWithCapacity:0];
NSArray *copyMutableArray = mutableArray.copy;//可變數(shù)組copy后,雖然為新的對(duì)象拟烫,但是對(duì)象類型是NSArray而不是NSMutableArray
NSLog(@"\n可變數(shù)組");
NSLog(@"\nmutableArray_p:%p,\ncopyMutableArray_p:%p",mutableArray,copyMutableArray);
// 可變數(shù)組
// mutableArray_p:0x604000457a90,
// copyMutableArray_p:0x6040000091f0
由此可得出結(jié)論
copy在拷貝可變對(duì)象的時(shí)候?yàn)樯羁截惛帽啵羁截惖膶?duì)象為全新的對(duì)象,有自己的內(nèi)存地址
拷貝不可變對(duì)象的時(shí)候?yàn)闇\拷貝硕淑,淺拷貝的對(duì)象共用同一塊內(nèi)存地址
mutablecopy
NSLog(@"-----------------mutableCopy-----------------");
NSString *str = @"字符串";
NSString *mutableCopyStr = str.mutableCopy;
NSLog(@"\n不可變字符串");
NSLog(@"\nstr_p:%p,\nmutableCopyStr:%p",str,mutableCopyStr);
// 不可變字符串
// str_p:0x10aa092c8,
// mutableCopyStr:0x600000041f20
NSArray *array = [NSArray array];
NSArray *mutablecopyArray = array.mutableCopy;
NSLog(@"\n不可變數(shù)組");
NSLog(@"\narray_p:%p,\nmutablecopyArray_p:%p",array,mutablecopyArray);
// 不可變數(shù)組
// array_p:0x6000000034e0,
// mutablecopyArray_p:0x604000256980
//可變對(duì)象
NSMutableString *mutablestr = [[NSMutableString alloc] initWithString:@"可變字符串"];
NSString *mutablecopyMutablestr = mutablestr.mutableCopy;
NSLog(@"\n可變字符串");
NSLog(@"\nmutablestr_p:%p,\nmutablecopyMutablestr_p:%p",mutablestr,mutablecopyMutablestr);
// 可變字符串
// mutablestr_p:0x600000241f50,
// mutablecopyMutablestr_p:0x60000005f440
NSMutableArray *mutableArray = [NSMutableArray arrayWithCapacity:0];
NSArray *mutablecopyMutableArray = mutableArray.mutableCopy;//
NSLog(@"\n可變數(shù)組");
NSLog(@"\nmutableArray_p:%p,\nmutablecopyMutableArray_p:%p",mutableArray,mutablecopyMutableArray);
// 可變數(shù)組
// mutableArray_p:0x604000255060,
// mutablecopyMutableArray_p:0x6040002555d0
由此可得出結(jié)論:mutablecopy都是深拷貝上渴,拷貝后的對(duì)象都有自己的內(nèi)存地址
當(dāng)容器內(nèi)有元素的時(shí)候,對(duì)對(duì)象的copy和mutablecopy會(huì)對(duì)里面的元素有啥影響呢喜颁?
NSMutableArray *mutableArray = [NSMutableArray arrayWithObjects:[NSMutableString stringWithString:@"1"],@"2", nil];
NSMutableArray *copyMutableArray = mutableArray.copy;
NSMutableArray *mutableCopyMutableArray = mutableArray.mutableCopy;
NSLog(@"mutableArray_p:%p,copyMutableArray_p:%p,mutableCopyMutableArray_p:%p",mutableArray,copyMutableArray,mutableCopyMutableArray);
//mutableArray_p:0x60000025c0e0,
//copyMutableArray_p:0x600000231980,
//mutableCopyMutableArray_p:0x60000025e120
//拷貝前
NSLog(@"mutableArray_object1_p:%p,mutableArray_object2_p:%p",mutableArray[0],mutableArray[1]);
//mutableArray_object1_p:0x60000025da90,
//mutableArray_object2_p:0x10d9b34c8
//copy后
NSLog(@"copyMutableArray_object1_p:%p,copyMutableArray_object2_p:%p",copyMutableArray[0],copyMutableArray[1]);
//copyMutableArray_object1_p:0x60000025da90,
//copyMutableArray_object2_p:0x10d9b34c8
//mutablecopy后
NSLog(@"mutableCopyMutableArray_object1_p:%p,mutableCopyMutableArray_object2_p:%p",mutableCopyMutableArray[0],mutableCopyMutableArray[1]);
//mutableCopyMutableArray_object1_p:0x60000025da90,
//mutableCopyMutableArray_object2_p:0x10d9b34c8
由此可見,copy和mutablecopy對(duì)于容器內(nèi)的元素是淺拷貝
如果你想對(duì)容器做完全拷貝(也就是容器深拷貝曹阔,元素也深拷貝)該怎么辦呢半开?可以使用歸檔的辦法(如果是自定義對(duì)象,需要實(shí)現(xiàn)NSCoding協(xié)議,不然會(huì)crash)赃份。
NSArray *array = [NSArray arrayWithObjects:[NSMutableString stringWithString:@"1"],@"2", nil];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:array];
NSArray *mutablecopyArray = [NSKeyedUnarchiver unarchiveObjectWithData:data];
NSLog(@"array_p:%p,array_objec1_p:%p,array_objec2_p:%p",array,array[0],array[1]);
//array_p:0x6040002386c0,
//array_objec1_p:0x60400025f590,
//array_objec2_p:0x10d17b4c8
NSLog(@"mutablecopyArray_p:%p,mutablecopyArray_objec1_p:%p,mutablecopyArray_objec2_p:%p",mutablecopyArray,mutablecopyArray[0],mutablecopyArray[1]);
//mutablecopyArray_p:0x604000238780,
//mutablecopyArray_objec1_p:0x60400005ce60,
//mutablecopyArray_objec2_p:0xa000000000000321
自定義對(duì)象的歸檔協(xié)議實(shí)現(xiàn)
#import "Student.h"
@interface Student ()<NSCoding>
@property (nonatomic,copy)NSString *age;
@end
@implementation Student
/**********編碼/解碼***********/
//解碼的時(shí)候調(diào)用
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
self.age = [aDecoder decodeObjectForKey:@"age"];
return self;
}
//編碼的時(shí)候調(diào)用
- (void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:self.age forKey:@"age"];
}
@end
自定義對(duì)象的copy和mutablecopy寂拆,需要實(shí)現(xiàn)NSCopying、NSMutableCopying協(xié)議抓韩,不然使用copy和mutablecopy方法會(huì)crash
#import "Student.h"
@interface Student ()<NSCopying,NSMutableCopying>
@property (nonatomic,copy)NSString *age;
@end
@implementation Student
/******淺拷貝/深拷貝*******/
- (id)copyWithZone:(NSZone *)zone
{
Student *s = [[[self class] allocWithZone:zone] init];
s.age = self.age;
return s;
}
- (id)mutableCopyWithZone:(NSZone *)zone
{
Student *s = [[[self class] allocWithZone:zone] init];
s.age = self.age;
return s;
}
@end