NSCopying和NSMutableCopying协议, copy和mutableCopy。
1、说明
某类想要支持copy操作,必须实现NSCopying协议,实现copyWithZone方法;
某类想要支持mutableCopy操作,必须实现NSMutableCopying协议,实现mutableCopyWithZone方法;
iOS系统中的一些类已实现了NSCopying或者NSMutableCopying协议的方法。如果向 未实现相应方法的类的实例(系统类 或者 自定义类)发送copy或者mutableCopy消息,会crash掉。
*** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '-[Person copyWithZone:]: unrecognized selector sent to instance 0x6081234314c0'
发送copy和mutableCopy消息,均是进行拷贝操作,
但是对
可变对象/不可变对象 的非容器类、
可变对象/不可变对象 的容器类、 复制的方式略有不同;
但有两点是相同的:
copy -------------> 拷贝出来的是不可变对象;
mutableCopy ---> 拷贝出来的是可变对象;
如下的操作会导致crash
NSMutableString *test1 = [[NSMutableString alloc]initWithString:@"111111111"];
NSMutableString *test2 = [test1 copy];
[test2 appendString:@"2222222"];
*** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '-[NSTaggedPointerString appendString:]: unrecognized selector sent to
2、系统非容器类
系统提供的非容器类中如NSString, NSMutableString,有如下特性:
[不可变对象 copy] ------------->指针拷贝;
[不可变对象 mutalbeCopy]---->内容拷贝(深拷贝);
NSString *test3 = @"111111";
NSString *test4 = [test3 copy];
NSMutableString *test5 = [test3 mutableCopy];
NSLog(@"test3 is %p, test4 is %p, tast5 is %p",test3,test4,test5);
test3 is 0x10d6bb3a8, test4 is 0x10d6bb3a8, tast5 is 0x600000073e80
向可变对象发送copy和mutableCopy消息,均是深拷贝,也就是说内容拷贝;
[可变对象 copy] ------------->内容拷贝(深拷贝);
[可变对象 mutalbeCopy]---->内容拷贝(深拷贝);
NSMutableString *test11 = [[NSMutableString alloc]initWithString:@"444444"];
NSString *test12 = [test11 copy];
NSMutableString *test13 = [test11 mutableCopy];
NSLog(@"test11 is %p, test12 is %p, tast13 is %p",test11,test12,test13);
test11 is 0x600000073e00, test12 is 0xa003434343434346, tast13 is 0x600000073dc0
3、系统容器类
系统提供的容器类中如 NSArray, NSDictionary 有如下特性:
不可变对象
copy,是浅拷贝,也就是说指针复制;
mutableCopy,是深复制,也就是说内容复制;
NSArray *array = [NSArray arrayWithObjects:@"1", nil];
NSArray *copyArray = [array copy];
NSMutableArray *mutableCopyArray = [array mutableCopy];
NSLog(@"array is %p, copyArray is %p, mutableCopyArray is %p", array, copyArray, mutableCopyArray);
array is 0x60800001e580, copyArray is 0x60800001e580, mutableCopyArray is 0x608000046ea0
可变对象
copy和mutableCopy均是单层深拷贝,也就是说单层的内容拷贝;
NSMutableArray *element = [NSMutableArray arrayWithObject:@1];
NSMutableArray *array = [NSMutableArray arrayWithObject:element];
NSArray *copyArray = [array copy];
NSMutableArray *mutableCopyArray = [array mutableCopy];
NSLog(@"array is %p, copyArray is %p, mutableCopyArray is %p", array, copyArray, mutableCopyArray);
[mutableCopyArray[0] addObject:@2];
NSLog(@"element is %@, array is %@, copyArray is %@, mutableCopyArray is %@", element,array,copyArray, mutableCopyArray);
2017-02-22 11:53:25.286 test[91520:3915695] array is 0x600000057670, copyArray is 0x600000000bc0, mutableCopyArray is 0x6080000582a0
2017-02-22 11:53:25.287 test[91520:3915695] element is (
1,
2
), array is (
(
1,
2
)
), copyArray is (
(
1,
2
)
), mutableCopyArray is (
(
1,
2
)
)
4、自定义的类
1、代码设计均 针对 业务需求..
2、对于自定义的类决定能否向对象发送copy和mutableCopy消息也是如此;
---1、@property 声明中用 copy 修饰
copy和strong在复制时候的区别,此处不讲引用计数的问题。
copy:拷贝一份不可变副本赋值给属性;所以当原对象值变化时,属性值不会变化;
strong:有可能指向一个可变对象, 如果这个可变对象在外部被修改了, 那么会影响该属性;
@interface Person : NSObject
@property (nonatomic, copy) NSString *familyname;
@property (nonatomic, strong) NSString *nickname;
@end
Person *p1 = [[Person alloc]init];
NSMutableString *familyname = [[NSMutableString alloc]initWithString:@"张三"];
p1.familyname = familyname;
[familyname appendString:@"是疯子"];
NSLog(@"p1.familyname is %@",p1.familyname);
NSMutableString *nickname = [[NSMutableString alloc]initWithString:@"二狗"];
p1.nickname = nickname;
[nickname appendString:@"是傻子"];
NSLog(@"p1.nickname is %@", p1.nickname);
2017-02-22 13:53:58.979 test[98299:3978965] p1.familyname is 张三
2017-02-22 13:53:58.979 test[98299:3978965] p1.nickname is 二狗是傻子
显然对nickname操作p1.nickname也一起变化了,因为它们在同一地址的里。
----2、类的对象的copy--
注意类的继承,结论:
1 、类直接继承自NSObject无需调用[super copyWithZone:zone]
2 、父类子类都实现了copy协议,子类需要调用[super copyWithZone:zone]
3、 父类没有实现copy协议,子类实现了,子类无需调用[super copyWithZone:zone]
4、copyWithZone方法中要调用[[[self class] alloc] init]来分配内存
5、NSCopying
NSCopying是对象拷贝的协议。
类的对象如果支持拷贝,该类应遵守并实现NSCopying协议。
NSCopying协议中的方法只有一个,如下:
- (id)copyWithZone:(NSZone *)zone {
Person *model = [[[self class] allocWithZone:zone] init];
model.firstName = self.firstName;
model.lastName = self.lastName;
//未公开的成员
model->_nickName = _nickName;
return model;
}
3、NSMutableCopying
当自定义的类有一个属性是可变对象时,对此属性复制时要执行mutableCopyWithZone操作。
- (id)copyWithZone:(NSZone *)zone {
AFHTTPRequestSerializer *serializer = [[[self class] allocWithZone:zone] init];
serializer.mutableHTTPRequestHeaders =[self.mutableHTTPRequestHeaders mutableCopyWithZone:zone];
serializer.queryStringSerializationStyle = self.queryStringSerializationStyle;
serializer.queryStringSerialization = self.queryStringSerialization;
return serializer;
}
网友评论