深拷贝和浅拷贝的概念
iOS中有深拷贝和浅拷贝的概念,那么何为深拷贝何为浅拷贝呢?
浅拷贝:浅拷贝并不拷贝对象本身,只是对指向对象的指针进行拷贝
深拷贝:直接拷贝对象到内存中一块区域,然后把新对象的指针指向这块内存
在iOS中并不是所有对象都支持Copy和MutableCopy,遵循NSCopying协议的类可以发送Copy协议,遵循NSMutableCopying协议的类可以发送MutableCopy消息。如果一个对象没有遵循这两个协议而发送Copy或者MutableCopy消息那么会发生异常。如果要遵循NSCopying协议,那么必须实现copyWithZone方法。如果要遵循NSMutableCopying协议那么必须实现mutableCopyWithZone方法。
可变对象和不可变对象分别调用Copy和MutableCopy方法的区别
1.系统非容器类对象
(1)调用copy和mutableCopy方法
NSString *str = @"123";
NSString *strCopy = [str copy];
NSMutableString *mustr = [str copy];
NSMutableString *muCopy = [str mutableCopy];
[mustr appendString:@"4"]; //会直接崩溃
[muCopy appendString:@"5"];
NSLog(@"str = %@ strCopy = %@ mustr = %@ muCopy = %@",str,strCopy,mustr,muCopy);
NSLog(@"str地址%p strCopy地址%p mustr地址%p muCopy地址%p",str ,strCopy,mustr,muCopy);
输出结果:
NSMutableString *mustr = [NSMutableString stringWithFormat:@"123"];
NSString *strCopy = [mustr copy];
NSMutableString *mutableStr = [mustr copy];
NSMutableString *mutableStrCopy = [mustr mutableCopy];
[mutableStr appendString:@"5"]; //会崩溃
[mutableStrCopy appendString:@"6"];
NSLog(@"mustr = %@ strCopy = %@ mutableStr = %@ mutableStrCopy = %@",mustr,strCopy,mutableStr,mutableStrCopy);
NSLog(@"mustr地址%p strCopy地址%p mutableStr地址%p mutableStrCopy地址%p",mustr,strCopy,mutableStr,mutableStrCopy);
输出结果:
从上面可以看出对系统非容器类不可变对象调用Copy方法其实只是把当前对象的指针指向了原对象的地址,而调用mutableCopy方法则是新分配了一块内存区域并把新对象的指针指向了这块区域。对于可变对象来说调用Copy和MutableCopy方法都会重新分配一块内存。但是copy和mutableCopy的区别在于copy在复制对象的时候其实是返回了一个不可变对象,因此当调用方法改变对象的时候会崩溃(个人猜测这一块可能和OC的多态性有关)。
2.系统容器类对象
(1)调用copy和mutableCopy方法
NSArray *array = [NSArray arrayWithObjects:@"1",@"2",@"3", nil];
NSArray *arrayCopy = [array copy];
NSMutableArray *muarray = [array copy];
NSMutableArray *copyArray = [array mutableCopy];
NSLog(@"array地址%p arrayCopy地址%p muarray地址%p copyArray地址%p",array,arrayCopy,muarray,copyArray);
输出结果:
NSMutableArray *muarray = [NSMutableArray arrayWithObjects:@"1",@"2",@"3", nil];
NSArray *array = [muarray copy];
NSArray *mutableCopy = [muarray mutableCopy];
NSMutableArray *muCopy = [muarray copy];
NSMutableArray *arrayCopy = [muarray mutableCopy];
NSLog(@"muarray地址%p array地址%p mutableCopy地址%p muCopy地址%p arrayCpy地址%p",muarray,array,mutableCopy,muCopy,arrayCopy);
输出结果:
从这上面看容器对象和非容器对象在分别调用Copy和MutableCopy时没有什么分别,不可变对象调用Copy方法只是增加了对原对象的指针的引用,调用MutableCopy方法是重新分配一块内存,然后把新对象指向新内存。而对于可变对象不管调用Copy还是MutableCopy都是新分配一块内存。但是虽然重新分配了一块内存,但是对象里面的数据依然是指针复制的,下面我们来看一段代码:
NSArray *array = [NSArray arrayWithObjects:[NSMutableString stringWithString:@"1"],@"2",@"3", nil];
NSArray *arrayCopy = [array copy];
NSArray *arrayMutableCopy = [array mutableCopy];
NSMutableArray *mutableArrayCopy = [array copy];
NSMutableArray *mutableArrayMutableCopy = [array mutableCopy];
NSLog(@"array地址%p arrayCopy地址%p arrayMutableCopy地址%p mutableArrayCopy地址%p mutableArrayCopy地址%p",array,arrayCopy,arrayMutableCopy,mutableArrayCopy,mutableArrayMutableCopy);
NSLog(@"array = %@ arrayCopy = %@ arrayMutableCopy = %@ mutableArrayCopy = %@ mutableArrayCopy = %@",array,arrayCopy,arrayMutableCopy,mutableArrayCopy,mutableArrayMutableCopy);
NSMutableString *mustr = array[0];
[mustr appendString:@"2"];
NSLog(@"array地址%p arrayCopy地址%p arrayMutableCopy地址%p mutableArrayCopy地址%p mutableArrayCopy地址%p",array,arrayCopy,arrayMutableCopy,mutableArrayCopy,mutableArrayMutableCopy);
NSLog(@"array = %@ arrayCopy = %@ arrayMutableCopy = %@ mutableArrayCopy = %@ mutableArrayCopy = %@",array,arrayCopy,arrayMutableCopy,mutableArrayCopy,mutableArrayMutableCopy);
输出结果:
未更改数组里面内容前的结果:
更改数组里面的内容的输出结果:
可以看到当更改原数组的值之后,所有新数组的值都更改了,即使调用了MutableCopy方法创建的新数组里面的值也因此更改,所以可以看出对于系统容器类对象,其元素对象始终是指针复制。