1.copy mutable copy
- NSString与NSMutableString
NSString *title = @"aaa";
NSLog(@"title %p",title);
NSLog(@"title copy %p",[title copy]);
NSLog(@"title mutable copy %p",[title mutableCopy]);
NSMutableString *titleMutable = [NSMutableString string];
NSLog(@"titleMutable %p",titleMutable);
NSLog(@"titleMutable copy %p",[titleMutable copy]);
NSLog(@"titleMutable copy %p",[titleMutable mutableCopy]);
打印如下
2016-08-10 10:46:59.251 Test1[1500:80994] title 0x108d74068
2016-08-10 10:46:59.252 Test1[1500:80994] title copy 0x108d74068
2016-08-10 10:46:59.252 Test1[1500:80994] title mutable copy 0x7fbedb721ca0
2016-08-10 10:46:59.252 Test1[1500:80994] titleMutable 0x7fbedb49ca40
2016-08-10 10:46:59.252 Test1[1500:80994] titleMutable copy 0x109a45280
2016-08-10 10:46:59.252 Test1[1500:80994] titleMutable copy 0x7fbedb4975b0
- NSValue
NSValue *value = [NSValue valueWithCGSize:(CGSizeMake(1, 1))];
NSLog(@"value %p",value);
NSLog(@"value copy %p",[value copy]);
// NSLog(@"value mutable copy %p",[value mutableCopy]); //无此方法
打印如下
2016-08-10 10:46:59.252 Test1[1500:80994] value 0x7fbedb4068e0
2016-08-10 10:46:59.252 Test1[1500:80994] value copy 0x7fbedb4068e0
- NSArray与 mutableArray
NSArray *array = [NSArray array];
NSLog(@"array %p",array);
NSLog(@"array copy %p",[array copy]);
NSLog(@"array mutable copy %p",[array mutableCopy]);
NSMutableArray *mutableArray = [NSMutableArray array];
NSLog(@"mutableArray %p",mutableArray);
NSLog(@"mutableArray copy %p",[mutableArray copy]);
NSLog(@"mutableArray mutable copy %p",[mutableArray mutableCopy]);
打印如下
2016-08-10 10:46:59.253 Test1[1500:80994] array 0x7fbedb501410
2016-08-10 10:46:59.253 Test1[1500:80994] array copy 0x7fbedb501410
2016-08-10 10:46:59.253 Test1[1500:80994] array mutable copy 0x7fbedb481620
2016-08-10 10:46:59.253 Test1[1500:80994] mutableArray 0x7fbedb71ba70
2016-08-10 10:46:59.253 Test1[1500:80994] mutableArray copy 0x7fbedb501410
2016-08-10 10:46:59.253 Test1[1500:80994] mutableArray mutable copy 0x7fbedb610b50
- NSDictionary与NSMutableDictionary
NSDictionary *dic = [NSDictionary dictionary];
NSLog(@"dic %p",dic);
NSLog(@"dic copy %p",[dic copy]);
NSLog(@"dic mutable copy %p",[dic mutableCopy]);
NSMutableDictionary *mutableDic = [NSMutableDictionary dictionary];
NSLog(@"mutableDic %p",mutableDic);
NSLog(@"mutableDic copy %p",[mutableDic copy]);
NSLog(@"mutableDic mutable copy %p",[mutableDic mutableCopy]);
打印如下
2016-08-10 10:46:59.253 Test1[1500:80994] dic 0x7fbedb5007a0
2016-08-10 10:46:59.253 Test1[1500:80994] dic copy 0x7fbedb5007a0
2016-08-10 10:46:59.253 Test1[1500:80994] dic mutable copy 0x7fbedb710000
2016-08-10 10:46:59.253 Test1[1500:80994] mutableDic 0x7fbedb51eee0
2016-08-10 10:46:59.254 Test1[1500:80994] mutableDic copy 0x7fbedb5007a0
2016-08-10 10:46:59.254 Test1[1500:80994] mutableDic mutable copy 0x7fbedb610b50
- 总结
不可变对象copy,不会分配新的内存空间
不可变对象mutableCopy,会分配新的内存空间
可变对象copy和mutableCopy,都会生成新的内存空间
以上结论仅对上面出现的Foundation类有效,其他对象要看其copy mutableCopy协议具体的实现方式
2.NSString类型的属性为什么用copy,而不用retain
是为了防止该属性赋一个NSMutableString类型的值,然后被外界改变,影响到该属性的值,比如
//声明两个属性
@property (nonatomic,copy) NSString *titleCopy;
@property (nonatomic,retain) NSString *titleRetain;
//implementation
NSMutableString *str = [NSMutableString stringWithString:@"aaa"];
self.titleCopy = str;
self.titleRetain = str;
[str appendString:@"bbb"];
NSLog(@"str : %@",str);
NSLog(@"title copy : %@",self.titleCopy);
NSLog(@"title retain : %@",self.titleRetain);
打印如下
**2016-08-10 11:10:20.829 Test1[1563:97056] str : aaabbb**
**2016-08-10 11:10:20.829 Test1[1563:97056] title copy : aaa**
**2016-08-10 11:10:20.829 Test1[1563:97056] title retain : aaabbb**
可见copy类型的属性的属性值并没有受原来值改变的影响
而retain类型的属性的属性值受到了影响
3.其他内存管理的语义
assign
只会针对纯量类型进行简单赋值操作
strong
一种拥有关系。设置方法会先保留新值,并释放旧值,再将新值设置上去
weak
非拥有关系。设置方法既不保留新值也不释放旧值,当属性所指对象时,属性值也会被清空
unsafe_unretained
非拥有关系,但当属性所指对象释放时不会自动清空。不安全。