深拷贝,和前拷贝,是一项非常重要的知识点。
浅拷贝:
NSString *aName=[[NSString alloc] initWithString:@"Tom"];
2012-12-07 23:06:37.708 DemoVideo[699:1b803] objArray:
2012-12-07 23:06:37.709 DemoVideo[699:1b803]=====================
2012-12-07 23:06:37.709 DemoVideo[699:1b803] name=Tom
2012-12-07 23:06:37.709 DemoVideo[699:1b803]email=Tom@yahoo.cn
2012-12-07 23:06:37.709 DemoVideo[699:1b803]=====================
2012-12-07 23:06:37.710 DemoVideo[699:1b803]=====================
2012-12-07 23:06:37.710 DemoVideo[699:1b803] name=LiLei
2012-12-07 23:06:37.710 DemoVideo[699:1b803]email=LiLei@yahoo.cn
2012-12-07 23:06:37.711 DemoVideo[699:1b803]=====================
2012-12-07 23:07:13.025 DemoVideo[699:1b803]=====================
2012-12-07 23:07:13.027 DemoVideo[699:1b803] name=Tom
2012-12-07 23:07:13.027 DemoVideo[699:1b803]email=Tom@yahoo.cn
2012-12-07 23:07:13.027 DemoVideo[699:1b803]=====================
2012-12-07 23:07:36.547 DemoVideo[699:1b803] objArray2:
2012-12-07 23:07:36.547 DemoVideo[699:1b803]=====================
2012-12-07 23:07:36.547 DemoVideo[699:1b803] name=Tom
2012-12-07 23:07:36.548 DemoVideo[699:1b803]email=Tom@yahoo.cn
2012-12-07 23:07:36.548 DemoVideo[699:1b803]=====================
2012-12-07 23:07:36.548 DemoVideo[699:1b803]=====================
2012-12-07 23:07:36.548 DemoVideo[699:1b803] name=LiLei
2012-12-07 23:07:36.549 DemoVideo[699:1b803]email=LiLei@yahoo.cn
2012-12-07 23:07:36.549 DemoVideo[699:1b803]=====================
2012-12-07 23:07:50.844 DemoVideo[699:1b803]=====================
2012-12-07 23:07:50.844 DemoVideo[699:1b803]name=CarBoon
2012-12-07 23:07:50.844 DemoVideo[699:1b803]email=CarBoon@yahoo.cn
2012-12-07 23:07:50.845 DemoVideo[699:1b803]=====================
2012-12-07 23:08:14.460 DemoVideo[699:1b803] objArray:
2012-12-07 23:08:14.460 DemoVideo[699:1b803]=====================
2012-12-07 23:08:14.461 DemoVideo[699:1b803]name=CarBoon
2012-12-07 23:08:14.461 DemoVideo[699:1b803]email=CarBoon@yahoo.cn
2012-12-07 23:08:14.461 DemoVideo[699:1b803]=====================
2012-12-07 23:08:14.461 DemoVideo[699:1b803]=====================
2012-12-07 23:08:14.461 DemoVideo[699:1b803] name=LiLei
2012-12-07 23:08:14.462 DemoVideo[699:1b803]email=LiLei@yahoo.cn
2012-12-07 23:08:14.462 DemoVideo[699:1b803]=====================
2012-12-07 23:08:44.689 DemoVideo[699:1b803] objArray2:
2012-12-07 23:08:45.493 DemoVideo[699:1b803]=====================
2012-12-07 23:08:45.493DemoVideo[699:1b803] name=CarBoon
2012-12-07 23:08:45.493DemoVideo[699:1b803] email=CarBoon@yahoo.cn
2012-12-07 23:08:45.493 DemoVideo[699:1b803]=====================
2012-12-07 23:08:46.021 DemoVideo[699:1b803]=====================
2012-12-07 23:08:46.021 DemoVideo[699:1b803] name=LiLei
2012-12-07 23:08:46.021 DemoVideo[699:1b803]email=LiLei@yahoo.cn
2012-12-07 23:08:46.021 DemoVideo[699:1b803]=====================
调用copy方法来拷贝不可变对象,或者调用mutableCopy方法拷贝可变对象时,
为新对象分配了内存空间,并且将对象数组中单个元素分别复制到新对象数组元素中。
对象默认为为浅复制,这种复制,仅仅是将引用从一个数组元素复制到另一个数组元素。
对,复制的是引用。所以,两个数组中的元素都指向内存中的同一个对象。
于是,这种情况就出现了,我们修改了原来数组中的对象,另一个数组中响应的元素也改变了。
深拷贝:内容拷贝,会产生新的对象。新对象计数器置为1,源对象计数器不变。
-(id)copyWithZone:(NSZone *)zone;
-(id)mutableCopyWithZone:(NSZone *)zone;
@interfaceAddressCard : NSObject<</span>NSCopying,NSMutableCopying>
#pragma mark- NSCopying
-(id)copyWithZone:(NSZone *)zone{
}
#pragma mark- NSMutableCopying
-(id)mutableCopyWithZone:(NSZone *)zone{
}
AddressCard *cloneCard=[card1copy];
2012-12-08 22:31:06.206 DemoVideo[464:1b803]=====================
2012-12-08 22:31:06.207 DemoVideo[464:1b803] name=Tom
2012-12-08 22:31:06.207 DemoVideo[464:1b803]email=Tom@yahoo.cn
2012-12-08 22:31:06.207 DemoVideo[464:1b803]=====================
2012-12-08 22:31:06.207 DemoVideo[464:1b803]=====================
2012-12-08 22:31:06.207 DemoVideo[464:1b803]name=changeValue
2012-12-08 22:31:06.207 DemoVideo[464:1b803]email=changeValue@yahoo.cn
2012-12-08 22:31:06.208 DemoVideo[464:1b803]=====================
2012-12-08 22:31:22.731DemoVideo[464:1b803] =====================
2012-12-08 22:31:22.731DemoVideo[464:1b803] name=Tom
2012-12-08 22:31:22.731DemoVideo[464:1b803] email=Tom@yahoo.cn
2012-12-08 22:31:22.731DemoVideo[464:1b803] =====================