深拷贝、浅拷贝

浅拷贝

nsstring * [email protected]”jack”;  常量区,没有产生新的对象

nsstring * str2=[str1 copy];

深拷贝

1、nsmutablestring *str1=[nsmutablestring stringwithformat:@”jack”];   在堆区

      nsmutablestring *str2=[str1 copy];

2、nsstring *[email protected]”jack”;   常量区

      nsstring *str2=[str1 mutablecopy];   堆区

3、nsmutablestring *str1=[nsmutablestring stringwithformat:@:”jack”];  在堆区

      nsmutablestring *str2=[str1 mutablecopy];

深拷贝、浅拷贝

深拷贝、浅拷贝

深拷贝、浅拷贝

@property参数中的copy,是对象拷贝,在setter方法中,release旧对象,拷贝一份新对象,再让指针指向新的一份对象,引用计数为1

深拷贝、浅拷贝

自己的类想要拷贝能力,需要遵守NSCopying协议,并实现copyWithZone方法,根据自己需要写深拷贝或者浅拷贝。

深拷贝、浅拷贝