1.深拷贝浅拷贝定义 :
A.浅拷贝只拷贝值,不开辟新的内存空间
B.深拷贝,不但拷贝值还开辟新的内存空间
2.Foundation框架下默认支持copy的对象类型有:
NString、NSArray、NSNumber、NSDictionar、NSMutableString、NSMutablerArray、NSMutableDictionary等
3.我这里只对 NSString/ NSMutableString/ NSArray /NSMutableArray 测试 (只做借鉴使用)
//1.string
//1.1 NSString测试
NSString *str = @"hanmengmeng";
NSString *copyStr = [str copy]; //浅拷贝
NSMutableString * copymuStr = [[NSMutableString alloc] init];
copymuStr = [str copy];//copy时 可变字符串等同于不可变字符 浅拷贝
NSLog(@"str %p,copyStr %p, copymuStr %p",str,copyStr,copymuStr);
NSString *mutableCopyStr = [str mutableCopy];//深拷贝
NSMutableString * mutableCopyMuStr = [[NSMutableString alloc] init];
mutableCopyMuStr = [str mutableCopy]; //深拷贝
NSLog(@"str %p, mutableCopyStr %p, mutableCopyStr %p",str,mutableCopyStr,mutableCopyMuStr);
image.png
//1.2 NSMutableString测试 (可变)
NSMutableString *mustr = [NSMutableString stringWithFormat:@"hahah"];
NSString *copystr = [mustr copy];//深拷贝
NSMutableString * copymustr = [[NSMutableString alloc] init];
copymustr = [mustr copy];//深拷贝
NSLog(@"mustr %p ,copystr %p, copymustr %p",mustr,copystr, copymustr);
NSString *mutableCopystr = [mustr mutableCopy]; //深拷贝
NSMutableString * mutableCopymuStr = [[NSMutableString alloc] init];
mutableCopymuStr = [mustr mutableCopy];//深拷贝
NSLog(@"mustr %p ,mutableCopystr %p, mutableCopymuStr %p",mustr,mutableCopystr, mutableCopymuStr);
image.png
//2.array
//2.1 NSArray 测试
NSArray *array = [NSArray arrayWithObjects:@"han",@"meng",@"meng", nil];
NSArray *copyArray = [array copy]; //浅拷贝
NSMutableArray *copymuArray = [[NSMutableArray alloc] init];
copymuArray = [array copy]; //浅拷贝
NSLog(@"array %p ,copyArray %p, copymuArray %p",array,copyArray, copymuArray);
NSArray *mutableCopyArray = [array mutableCopy]; //深拷贝
NSMutableArray *mutableCopymuArray = [[NSMutableArray alloc] init];
mutableCopymuArray = [array mutableCopy]; //深拷贝
NSLog(@"array %p ,mutableCopyArray %p, mutableCopymuArray %p",array,mutableCopyArray, mutableCopymuArray);
image.png
//2.2 NSMutableArray 测试
NSMutableArray * marray =[[NSMutableArray alloc] initWithObjects:@"a",@"b",@"c" ,nil];
NSArray *copyMArray = [marray copy]; //深拷贝
NSMutableArray *copymMarray = [[NSMutableArray alloc] init];
copymMarray = [marray copy]; //深拷贝
NSLog(@"array %p ,copyMArray %p, copymMarray %p",array,copyMArray, copymMarray);
NSArray *mutableCopyMArray = [marray mutableCopy]; //深拷贝
NSMutableArray *mutableCopymMarray = [[NSMutableArray alloc] init];
mutableCopymMarray = [marray mutableCopy]; //深拷贝
NSLog(@"array %p ,mutableCopyMArray %p, mutableCopymMarray %p",array,mutableCopyMArray, mutableCopymMarray);
image.png
4. 不可变类型的copy是浅拷贝 可变的类型copy和mutableCopy都是深拷贝 (个人观点,仅供参考)
5.附加 脑袋抽抽测试
// 3.对象 通过实现NSCopying协议如下的唯一方法来达到对象拷贝的效果 所以不能用copy 或者 mutableCopy
NSObject * obj = [[UIView alloc] init];
NSObject * copyObj = [obj copy];
NSObject * mutableCopyobj = [obj mutableCopy];
NSLog(@"%p,%p,%p",obj,copyObj,mutableCopyobj);
//要用这两个方法,这是代理方法,别忘了遵守协议
- (id)copyWithZone:(nullable NSZone *)zone;
- (id)mutableCopyWithZone:(nullable NSZone *)zone;
网友评论