iOS 浅谈:深.浅拷贝与copy.strong
2016-05-15 22:19
621 查看
http://help.3g.163.com/15/0908/11/B305R99R00964KAD.html
深.浅拷贝
copy mutableCopy NSString
NSString *string = @"汉斯哈哈哈";// 没有产生新对象NSString *copyString = [string copy];// 产生新对象NSMutableString *mutableCopyString = [string mutableCopy];NSLog(@"string = %p copyString = %p mutableCopyString = %p", string, copyString, mutableCopyString);
copy mutableCopy NSMutableString
NSMutableString *string = [NSMutableString stringWithString:@"汉斯哈哈哈"];// 产生新对象NSString *copyString = [string copy];// 产生新对象NSMutableString *mutableCopyString = [string mutableCopy];NSLog(@"string = %p copyString = %p mutableCopyString = %p", string, copyString, mutableCopyString);
结论:
注意:其他对象NSArray、NSMutableArray 、NSDictionary、NSMutableDictionary一样适用
copy NSObject
HSPerson *p = [[HSPerson alloc] init];p.age = 20;p.height = 170.0;HSPerson *copyP = [p copy]; // 这里崩溃
崩溃:
看崩溃信息HSPerson应该先实现:
- (id)copyWithZone:(NSZone *)zone;
测试:
#import "HSPerson.h"@interface HSPerson()@end@implementation HSPerson- (id)copyWithZone:(NSZone *)zone{ return @"汉斯哈哈哈";}@end HSPerson *p = [[HSPerson alloc] init];p.age = 20;p.height = 170.0;HSPerson *copyP = [p copy];NSLog(@"copyP: %@", copyP);
可以看出copyWithZone重新分配新的内存空间,则:
- (id)copyWithZone:(NSZone *)zone{ HSPerson *person = [[HSPerson allocWithZone:zone] init]; return person;// 有些人可能下面alloc,重新初始化空间,但这方法已给你分配了zone,自己就无需再次alloc内存空间了// HSPerson *person = [[HSPerson alloc] init];}
HSPerson *p = [[HSPerson alloc] init];p.age = 20;p.height = 170.0;HSPerson *copyP = [p copy];NSLog(@"p = %p copyP = %p", p, copyP);NSLog(@"age = %d height = %f", copyP.age, copyP.height);
虽然copy了份新的对象,然而age,height值并未copy,那么:
- (id)copyWithZone:(NSZone *)zone{ HSPerson *person = [[HSPerson allocWithZone:zone] init]; person.age = self.age; person.height = self.height; // 这里self其实就要被copy的那个对象,很显然要自己赋值给新对象,所以这里可以控制copy的属性 return person;}
这时你会想,有NSMutableCopying?没错,是有这货:
- (id)mutableCopyWithZone:(NSZone *)zone{ HSPerson *person = [[HSPerson allocWithZone:zone] init]; person.age = self.age; person.height = self.height; return person;}
NSCopying、NSMutableCopying有啥区别?
其实感觉没必要有NSMutableCopying,因为压根就没可变的HSPerson,但如果该对象有其他行为,可以借用NSMutableCopying实现,哈哈哈
copy.strong
说完深浅拷贝,理解copy.strong就轻松多了!
copy
#import @interface HSPerson : NSObject@property (nonatomic, copy) NSString *name;@end NSMutableString *string = [NSMutableString stringWithFormat:@"汉斯哈哈哈"];HSPerson *person = [[HSPerson alloc] init];person.name = string;// 不能改变person.name的值,因为其内部copy新的对象[string appendString:@" hans"]; NSLog(@"name = %@", person.name);
property copy 实际上就对name干了这个:
- (void)setName:(NSString *)name{ _name = [name copy];}
假设name为NSMutableString,会发生什么事?
@property (nonatomic, copy) NSMutableString *name;
这样会挨骂哦,实际上内部还是:
- (void)setName:(NSMutableString *)name{ _name = [name copy];}
copy出来的仍然是不可变字符!如果有人用NSMutableString的方法,就会崩溃:
strong
@property (nonatomic, strong) NSString *name; NSMutableString *string = [NSMutableString stringWithFormat:@"汉斯哈哈哈"];HSPerson *person = [[HSPerson alloc] init];person.name = string;// 可以改变person.name的值,因为其内部没有生成新的对象[string appendString:@" hans"];NSLog(@"name = %@", person.name);
总结:用copy与strong取决于需求,如果不希望被外界更改用copy,反之用strong
深.浅拷贝
copy mutableCopy NSString
NSString *string = @"汉斯哈哈哈";// 没有产生新对象NSString *copyString = [string copy];// 产生新对象NSMutableString *mutableCopyString = [string mutableCopy];NSLog(@"string = %p copyString = %p mutableCopyString = %p", string, copyString, mutableCopyString);
copy mutableCopy NSMutableString
NSMutableString *string = [NSMutableString stringWithString:@"汉斯哈哈哈"];// 产生新对象NSString *copyString = [string copy];// 产生新对象NSMutableString *mutableCopyString = [string mutableCopy];NSLog(@"string = %p copyString = %p mutableCopyString = %p", string, copyString, mutableCopyString);
结论:
注意:其他对象NSArray、NSMutableArray 、NSDictionary、NSMutableDictionary一样适用
copy NSObject
HSPerson *p = [[HSPerson alloc] init];p.age = 20;p.height = 170.0;HSPerson *copyP = [p copy]; // 这里崩溃
崩溃:
看崩溃信息HSPerson应该先实现:
- (id)copyWithZone:(NSZone *)zone;
测试:
#import "HSPerson.h"@interface HSPerson()@end@implementation HSPerson- (id)copyWithZone:(NSZone *)zone{ return @"汉斯哈哈哈";}@end HSPerson *p = [[HSPerson alloc] init];p.age = 20;p.height = 170.0;HSPerson *copyP = [p copy];NSLog(@"copyP: %@", copyP);
可以看出copyWithZone重新分配新的内存空间,则:
- (id)copyWithZone:(NSZone *)zone{ HSPerson *person = [[HSPerson allocWithZone:zone] init]; return person;// 有些人可能下面alloc,重新初始化空间,但这方法已给你分配了zone,自己就无需再次alloc内存空间了// HSPerson *person = [[HSPerson alloc] init];}
HSPerson *p = [[HSPerson alloc] init];p.age = 20;p.height = 170.0;HSPerson *copyP = [p copy];NSLog(@"p = %p copyP = %p", p, copyP);NSLog(@"age = %d height = %f", copyP.age, copyP.height);
虽然copy了份新的对象,然而age,height值并未copy,那么:
- (id)copyWithZone:(NSZone *)zone{ HSPerson *person = [[HSPerson allocWithZone:zone] init]; person.age = self.age; person.height = self.height; // 这里self其实就要被copy的那个对象,很显然要自己赋值给新对象,所以这里可以控制copy的属性 return person;}
这时你会想,有NSMutableCopying?没错,是有这货:
- (id)mutableCopyWithZone:(NSZone *)zone{ HSPerson *person = [[HSPerson allocWithZone:zone] init]; person.age = self.age; person.height = self.height; return person;}
NSCopying、NSMutableCopying有啥区别?
其实感觉没必要有NSMutableCopying,因为压根就没可变的HSPerson,但如果该对象有其他行为,可以借用NSMutableCopying实现,哈哈哈
copy.strong
说完深浅拷贝,理解copy.strong就轻松多了!
copy
#import @interface HSPerson : NSObject@property (nonatomic, copy) NSString *name;@end NSMutableString *string = [NSMutableString stringWithFormat:@"汉斯哈哈哈"];HSPerson *person = [[HSPerson alloc] init];person.name = string;// 不能改变person.name的值,因为其内部copy新的对象[string appendString:@" hans"]; NSLog(@"name = %@", person.name);
property copy 实际上就对name干了这个:
- (void)setName:(NSString *)name{ _name = [name copy];}
假设name为NSMutableString,会发生什么事?
@property (nonatomic, copy) NSMutableString *name;
这样会挨骂哦,实际上内部还是:
- (void)setName:(NSMutableString *)name{ _name = [name copy];}
copy出来的仍然是不可变字符!如果有人用NSMutableString的方法,就会崩溃:
strong
@property (nonatomic, strong) NSString *name; NSMutableString *string = [NSMutableString stringWithFormat:@"汉斯哈哈哈"];HSPerson *person = [[HSPerson alloc] init];person.name = string;// 可以改变person.name的值,因为其内部没有生成新的对象[string appendString:@" hans"];NSLog(@"name = %@", person.name);
总结:用copy与strong取决于需求,如果不希望被外界更改用copy,反之用strong
相关文章推荐
- iOS安全入门
- ios objc 方法调用记录插件(支持arm64+devlog):itracer v1.2
- 【iOS--pod】使用初体验 --> libjingle_peerconnection
- 慧都独家揭晓国外十大Mac和iOS应用开发工具
- [转]iOS开发中@property的属性weak nonatomic strong readonly等介绍
- 【开源】专业K线绘制[K线主副图、趋势图、成交量、滚动、放大缩小、MACD、KDJ等)
- IOS函数可变参数
- iOS类似探探交友滑动动画分析
- iOS设计模式(一) 原型模式
- iOS framework 使用单元测试报错解决
- iOS MVVM 框架设计 和 MVC 框架
- iOS中添加毛玻璃状态的视图效果
- ios 真机调试
- ios 自动布局视频教程
- iOS开发系列--视图切换
- iOS开发————数据本地化方式之SSKeychain
- iOS开发Runtime那点事
- iOS开发————数据本地化方式之NSUserDefaults
- ios学习资源
- oc开发笔记5 iOS addsubview 使用过场动画