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]); //無(wú)此方法
打印如下
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
- 總結(jié)
不可變對(duì)象copy尉尾,不會(huì)分配新的內(nèi)存空間
不可變對(duì)象mutableCopy隅居,會(huì)分配新的內(nèi)存空間
可變對(duì)象copy和mutableCopy瞳别,都會(huì)生成新的內(nèi)存空間
以上結(jié)論僅對(duì)上面出現(xiàn)的Foundation類有效莺琳,其他對(duì)象要看其copy mutableCopy協(xié)議具體的實(shí)現(xiàn)方式
2.NSString類型的屬性為什么用copy,而不用retain
是為了防止該屬性賦一個(gè)NSMutableString類型的值蛉幸,然后被外界改變赏酥,影響到該屬性的值,比如
//聲明兩個(gè)屬性
@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**
可見(jiàn)copy類型的屬性的屬性值并沒(méi)有受原來(lái)值改變的影響
而retain類型的屬性的屬性值受到了影響
3.其他內(nèi)存管理的語(yǔ)義
assign
只會(huì)針對(duì)純量類型進(jìn)行簡(jiǎn)單賦值操作
strong
一種擁有關(guān)系赖淤。設(shè)置方法會(huì)先保留新值,并釋放舊值医增,再將新值設(shè)置上去
weak
非擁有關(guān)系慎皱。設(shè)置方法既不保留新值也不釋放舊值,當(dāng)屬性所指對(duì)象時(shí)叶骨,屬性值也會(huì)被清空
unsafe_unretained
非擁有關(guān)系,但當(dāng)屬性所指對(duì)象釋放時(shí)不會(huì)自動(dòng)清空茫多。不安全。