iOS之属性修饰符 retain、strong和copy区别测试
时不时会有点迷惑属性修饰符retain、strong、copy三者之间的区别,还是把测试过程记录下来好一点!
2、给retain、strong、copy修饰的字符串属性赋值指针变化测试例子
3、字符串调用copy、mutableCopy方法给字符串赋值指针变化例子
一、属性修饰符retain、strong、copy修饰字符串测试
先看代码,创建一个Person类,定义属性
#import <Foundation/Foundation.h> @interface Person : NSObject @property (nonatomic, retain) NSString *strRetain;
@property (nonatomic, strong) NSString *strStrong;
@property (nonatomic, copy) NSString *strCopy;
@property (nonatomic, retain) NSMutableString *strMutableRetain;
@property (nonatomic, strong) NSMutableString *strMutableStrong;
@property (nonatomic, copy) NSMutableString *strMutableCopy; @end
测试代码:
Person *per = [[Person alloc] init];
NSString *name = @"原XM";
[per setStrRetain:name];
[per setStrStrong:name];
per.strCopy = name; NSMutableString *name2 = [[NSMutableString alloc] initWithString:@"原XM2"];
per.strMutableRetain = name2;
per.strMutableStrong = name2;
per.strMutableCopy = name2; NSLog(@"------ 改变之前打印 ------");
NSLog(@"per.strRetain: %@, ", per.strRetain);
NSLog(@"per.strStrong: %@", per.strStrong);
NSLog(@"per.strCopy: %@", per.strCopy);
NSLog(@"per.strMutableRetain: %@", per.strMutableRetain);
NSLog(@"per.strMutableStrong: %@", per.strMutableStrong);
NSLog(@"per.strMutableCopy: %@", per.strMutableCopy); name = @"新XM";
[name2 appendString:@"新XM2"]; NSLog(@"------- 改变之后打印 -------");
NSLog(@"per.strRetain: %@", per.strRetain);
NSLog(@"per.strStrong: %@", per.strStrong);
NSLog(@"per.strCopy: %@", per.strCopy);
NSLog(@"per.strMutableRetain: %@", per.strMutableRetain);
NSLog(@"per.strMutableStrong: %@", per.strMutableStrong);
NSLog(@"per.strMutableCopy: %@", per.strMutableCopy);
打印日志:
------ 改变之前打印 ------
per.strRetain: 原XM,
per.strStrong: 原XM
per.strCopy: 原XM
per.strMutableRetain: 原XM2
per.strMutableStrong: 原XM2
per.strMutableCopy: 原XM2
------- 改变之后打印 -------
per.strRetain: 原XM
per.strStrong: 原XM
per.strCopy: 原XM
per.strMutableRetain: 原XM2新XM2
per.strMutableStrong: 原XM2新XM2
per.strMutableCopy: 原XM2
结论:
1、对于不可变对象来说:retain、strong、copy三者的作用是一样的,即当引用的原对象值改变后,其他引用该对象的属性值不会受影响,还是保持原来的值;
2、对于可变对象来说:retain、strong和copy的作用就有区别了,使用retain、strong修饰的属性,当引用的原对象值改变后,其他引用该对象的属性值会一起跟着变化,而copy修饰的属性的值还是保持原样。copy的作用主要也是体现在这里: 让属性值不会随着原引用对象的值改变而改变;
3、retain和strong的区别:作用是一样的,只是写法上的区别。在非arc机制时,是用retain关键字修饰;在arc机制后,一般都用strong关键字来代替retain了
4、根本原因是:给字符串属性赋值不可变字符串,retain、strong、copy修饰的可变和不可变字符串属性都是指针拷贝;
给字符串属性赋值可变字符串,retain、strong修饰的可变和不可变字符串属性是指针拷贝,而copy修饰的可变和不可变字符串属性都是内容拷贝。
(说明一下:第一种情况,给字符串属性赋值不可变字符串,虽然retain、strong、copy修饰的属性都是指针拷贝,但是因为源数据是不可变字符串,导致源数据值改变后,相应的就是另外一个指针地址了,但是这些属性还是原来的指针,所以值也是原来的值不会改变;
第二种情况,给字符串属性赋值可变字符串,retain、strong修饰的属性是指针拷贝,所以源可变字符串值改变后,指针没变,则retain、strong修饰的属性指针与源字符串一样,所以值也跟着变化;而copy修饰的字符串属性是内容拷贝,源字符串的值改变和属性已经没有关系了,因而不会引起属性值改变。
所以,我们如果从效果来看的话,想要源字符串值的改变不引起属性字符串值的改变,只有当源字符串是可变字符串时,属性才需要用copy修饰,其他情况使用strong、retain修饰效果是一样的,虽然是指针拷贝,但是源字符串值的改变不会引起属性字符串值的改变。当然我们还是要理解他们的区别才是王道。所以一般字符串属性都是用copy来修饰,靠谱的用法!
)
指针变化见如下代码
------ 再次打印,这次把指针的变化都打印出来,一目了然,彻底明白 ------
NSString *str1 = @"oneone壹亿一";
Person *obj = [[Person alloc] init];
obj.strStrong = str1; //指针拷贝
obj.strRetain = str1; //指针拷贝
obj.strCopy = str1; //指针拷贝
obj.strMutableStrong = str1; //指针拷贝
obj.strMutableRetain = str1; //指针拷贝
obj.strMutableCopy = str1; //指针拷贝
NSLog(@"指针1:%p, %p, %p, %p, %p, %p, %p", str1, obj.strStrong, obj.strRetain, obj.strCopy, obj.strMutableStrong, obj.strMutableRetain, obj.strMutableCopy);
NSLog(@"值1:%@, %@, %@, %@, %@, %@, %@", str1, obj.strStrong, obj.strRetain, obj.strCopy, obj.strMutableStrong, obj.strMutableRetain, obj.strMutableCopy);
/*
指针1:0x10c861e30, 0x10c861e30, 0x10c861e30, 0x10c861e30, 0x10c861e30, 0x10c861e30, 0x10c861e30
值1:oneone壹亿一, oneone壹亿一, oneone壹亿一, oneone壹亿一, oneone壹亿一, oneone壹亿一, oneone壹亿一
*/ str1 = @"twotwo我";
NSLog(@"指针2:%p, %p, %p, %p, %p, %p, %p", str1, obj.strStrong, obj.strRetain, obj.strCopy, obj.strMutableStrong, obj.strMutableRetain, obj.strMutableCopy);
NSLog(@"值2:%@, %@, %@, %@, %@, %@, %@", str1, obj.strStrong, obj.strRetain, obj.strCopy, obj.strMutableStrong, obj.strMutableRetain, obj.strMutableCopy);
/*
指针2:0x10c861e90, 0x10c861e30, 0x10c861e30, 0x10c861e30, 0x10c861e30, 0x10c861e30, 0x10c861e30
值2:twotwo我, oneone壹亿一, oneone壹亿一, oneone壹亿一, oneone壹亿一, oneone壹亿一, oneone壹亿一
*/ NSString *str2 = [[NSString alloc] initWithFormat:@"three哈%d", ];
obj.strStrong = str2; //指针拷贝
obj.strRetain = str2; //指针拷贝
obj.strCopy = str2; //指针拷贝
obj.strMutableStrong = str2; //指针拷贝
obj.strMutableRetain = str2; //指针拷贝
obj.strMutableCopy = str2; //指针拷贝
NSLog(@"指针3:%p, %p, %p, %p, %p, %p, %p", str2, obj.strStrong, obj.strRetain, obj.strCopy, obj.strMutableStrong, obj.strMutableRetain, obj.strMutableCopy);
NSLog(@"值3:%@, %@, %@, %@, %@, %@, %@", str2, obj.strStrong, obj.strRetain, obj.strCopy, obj.strMutableStrong, obj.strMutableRetain, obj.strMutableCopy);
/*
指针3:0x7fb9f9e04650, 0x7fb9f9e04650, 0x7fb9f9e04650, 0x7fb9f9e04650, 0x7fb9f9e04650, 0x7fb9f9e04650, 0x7fb9f9e04650
值3:three哈8, three哈8, three哈8, three哈8, three哈8, three哈8, three哈8
*/ str2 = @"four啊";
NSLog(@"指针4:%p, %p, %p, %p, %p, %p, %p", str2, obj.strStrong, obj.strRetain, obj.strCopy, obj.strMutableStrong, obj.strMutableRetain, obj.strMutableCopy);
NSLog(@"值4:%@, %@, %@, %@, %@, %@, %@", str2, obj.strStrong, obj.strRetain, obj.strCopy, obj.strMutableStrong, obj.strMutableRetain, obj.strMutableCopy);
/*
指针4:0x10c861f50, 0x7fb9f9e04650, 0x7fb9f9e04650, 0x7fb9f9e04650, 0x7fb9f9e04650, 0x7fb9f9e04650, 0x7fb9f9e04650
值4:four啊, three哈8, three哈8, three哈8, three哈8, three哈8, three哈8
*/ NSLog(@" ---- start NSMutableString -------");
NSMutableString *str3 = [[NSMutableString alloc] initWithString:@"five神"];
obj.strStrong = str3; //指针拷贝
obj.strRetain = str3; //指针拷贝
obj.strCopy = str3; //内容拷贝
obj.strMutableStrong = str3; //指针拷贝
obj.strMutableRetain = str3; //指针拷贝
obj.strMutableCopy = str3; //内容拷贝
/*
.语法和setter方法设置值的效果是一样的
[obj setStrStrong: str3];
[obj setStrRetain: str3];
[obj setStrCopy: str3];
[obj setStrMutableStrong: str3];
[obj setStrMutableRetain: str3];
[obj setStrMutableCopy: str3];
*/
NSLog(@"指针5:%p, %p, %p, %p, %p, %p, %p", str3, obj.strStrong, obj.strRetain, obj.strCopy, obj.strMutableStrong, obj.strMutableRetain, obj.strMutableCopy);
NSLog(@"值5:%@, %@, %@, %@, %@, %@, %@", str3, obj.strStrong, obj.strRetain, obj.strCopy, obj.strMutableStrong, obj.strMutableRetain, obj.strMutableCopy);
/*
指针5:0x7fb9f9d9b920, 0x7fb9f9d9b920, 0x7fb9f9d9b920, 0x7fb9f9d9b6c0, 0x7fb9f9d9b920, 0x7fb9f9d9b920, 0x7fb9f9d0b930
值5:five神, five神, five神, five神, five神, five神, five神
*/ [str3 appendString:@"明天"];
NSLog(@"指针6:%p, %p, %p, %p, %p, %p, %p", str3, obj.strStrong, obj.strRetain, obj.strCopy, obj.strMutableStrong, obj.strMutableRetain, obj.strMutableCopy);
NSLog(@"值6:%@, %@, %@, %@, %@, %@, %@", str3, obj.strStrong, obj.strRetain, obj.strCopy, obj.strMutableStrong, obj.strMutableRetain, obj.strMutableCopy);
/*
指针6:0x7fb9f9d9b920, 0x7fb9f9d9b920, 0x7fb9f9d9b920, 0x7fb9f9d9b6c0, 0x7fb9f9d9b920, 0x7fb9f9d9b920, 0x7fb9f9d0b930
值6:five神明天, five神明天, five神明天, five神, five神明天, five神明天, five神
*/
再次描述下结论:
给字符串属性赋值不可变字符串,retain、strong、copy修饰的可变和不可变字符串属性都是指针拷贝;
给字符串属性赋值可变字符串,retain、strong修饰的可变和不可变字符串属性是指针拷贝,而copy修饰的可变和不可变字符串属性都是内容拷贝。
关键变化,截图标识一下:
--------------- 一 end ---------------
二、字符串调用copy、mutableCopy方法给字符串赋值
看看字符串调用copy和mutableCopy方法给字符串赋值,还有给属性修饰符修饰的字符串属性赋值指针变化
先看代码1:
//不可变字符串
NSString *str = @"aa啊";
NSString *str1 = [str copy]; //指针拷贝
NSString *str2 = [str mutableCopy]; //内容拷贝
NSMutableString *str3 = [str copy]; //指针拷贝
NSMutableString *str4 = [str mutableCopy]; //内容拷贝 NSLog(@"指针1:%p, %p, %p, %p, %p", str, str1, str2, str3, str4);
/*
指针1:0x1041093f0, 0x1041093f0, 0x7f951840b6e0, 0x1041093f0, 0x7f951840dfe0
*/ //可变字符串
NSMutableString *str5 = [NSMutableString stringWithString:@"bb吧"];
str1 = [str5 copy]; //内容拷贝
str2 = [str5 mutableCopy]; //内容拷贝
str3 = [str5 copy]; //内容拷贝
str4 = [str5 mutableCopy]; //内容拷贝 NSLog(@"指针2:%p, %p, %p, %p, %p", str5, str1, str2, str3, str4);
NSLog(@"值2:%@, %@, %@, %@, %@", str5, str1, str2, str3, str4);
/*
指针2:0x7f9518410af0, 0x7f9518435840, 0x7f9518416390, 0x7f9518403a00, 0x7f951840b6e0
值2:bb吧, bb吧, bb吧, bb吧, bb吧
*/ [str5 appendString:@"大大"];
NSLog(@"指针3:%p, %p, %p, %p, %p", str5, str1, str2, str3, str4);
NSLog(@"值3:%@, %@, %@, %@, %@", str5, str1, str2, str3, str4);
/*
指针3:0x7f9518410af0, 0x7f9518435840, 0x7f9518416390, 0x7f9518403a00, 0x7f951840b6e0
值3:bb吧大大, bb吧, bb吧, bb吧, bb吧
*/
结论:1)、不可变字符串使用copy方法赋值给其他可变或不可变字符串,都是指针拷贝;
2)、不可变字符串使用mutableCopy方法赋值给可变或不可变字符串,和可变字符串使用copy方法或者mutableCopy方法赋值给其他可变或不可变字符串,都是内容拷贝
再看代码2:
Person *obj = [[Person alloc] init];
NSString *str = @"y壹"; //不可变字符串调用copy方法,全部指针拷贝
obj.strStrong = [str copy];
obj.strCopy = [str copy];
obj.strMutableStrong = [str copy];
obj.strMutableCopy = [str copy];
NSLog(@"指针1:%p, %p, %p, %p, %p", str, obj.strStrong, obj.strCopy, obj.strMutableStrong, obj.strMutableCopy);
//指针1:0x101be34f0, 0x101be34f0, 0x101be34f0, 0x101be34f0, 0x101be34f0 //不可变字符串调用mutableCopy方法,全部内容拷贝
str = @"e二";
obj.strStrong = [str mutableCopy];
obj.strCopy = [str mutableCopy];
obj.strMutableStrong = [str mutableCopy];
obj.strMutableCopy = [str mutableCopy];
NSLog(@"指针2:%p, %p, %p, %p, %p", str, obj.strStrong, obj.strCopy, obj.strMutableStrong, obj.strMutableCopy);
//指针2:0x101be3510, 0x7ff939498790, 0x7ff9394a2020, 0x7ff9394970c0, 0x7ff939405d40 //可变字符串调用copy方法,全部内容拷贝
NSMutableString *str2 = [NSMutableString stringWithString:@"h叁"];
obj.strStrong = [str2 copy];
obj.strCopy = [str2 copy];
obj.strMutableStrong = [str2 copy];
obj.strMutableCopy = [str2 copy];
NSLog(@"指针3:%p, %p, %p, %p, %p", str2, obj.strStrong, obj.strCopy, obj.strMutableStrong, obj.strMutableCopy);
//指针3:0x7ff93951f540, 0x7ff93954a100, 0x7ff9395288c0, 0x7ff939516ca0, 0x7ff939548db0 //可变字符串调用mutableCopy方法,全部内容拷贝
[str2 appendString:@"叁2"];
obj.strStrong = [str2 mutableCopy];
obj.strCopy = [str2 mutableCopy];
obj.strMutableStrong = [str2 mutableCopy];
obj.strMutableCopy = [str2 mutableCopy];
NSLog(@"指针4:%p, %p, %p, %p, %p", str2, obj.strStrong, obj.strCopy, obj.strMutableStrong, obj.strMutableCopy);
//指针4:0x7ff93951f540, 0x7ff93951f320, 0x7ff93954a100, 0x7ff9395627b0, 0x7ff939516ca0
结论:1)、不可变字符串调用copy给strong, copy修饰的属性赋值,都是指针拷贝;
2)、不可变字符串调用mutableCopy, 和可变字符串调用copy,和可变字符串调用mutableCopy方法给strong、copy修饰的属性赋值,都是内容拷贝
所以说属性修饰符copy不一定代表着深拷贝,碰上数据源为不可变数据则是浅拷贝(指针拷贝),碰上数据源是可变数据则是深拷贝(内容拷贝);
NSObject的方法copy也不一定代表着浅拷贝,碰上数据源为不可变数据则是浅拷贝,碰上数据源是可变数据则是深拷贝;
NSObject的方法mutableCopy不管数据源是不可变还是可变,都是深拷贝;
这以上几个结论适用字符串,也适用下面的数组测试!
---------------- 二 end --------------
三、给修饰符strong、copy修饰的数组赋值测试
先创建一个有数组属性的模型类TestArr :
@interface TestArr : NSObject //测试数组的copy和strong属性
@property (nonatomic, strong) NSArray *arrStrong;
@property (nonatomic, copy) NSArray *arrCopy; @property (nonatomic, strong) NSMutableArray *arrMutableStrong;
@property (nonatomic, copy) NSMutableArray *arrMutableCopy; @end
1、数组直接赋值给修饰符strong、copy修饰的属性
NSArray *arr1 = [NSArray arrayWithObjects:@"twotwo2", nil];
TestArr *arrObj = [[TestArr alloc] init];
// arrObj.arrStrong = arr1; //指针拷贝
// arrObj.arrCopy = arr1; //指针拷贝
// arrObj.arrMutableStrong = arr1; //指针拷贝
// arrObj.arrMutableCopy = arr1; //指针拷贝
//.语法和setter方法设置值效果一样
[arrObj setArrStrong:arr1];
[arrObj setArrCopy:arr1];
[arrObj setArrMutableStrong:arr1];
[arrObj setArrMutableCopy:arr1]; NSLog(@"指针9:%p, %p, %p, %p, %p", arr1, arrObj.arrStrong, arrObj.arrCopy, arrObj.arrMutableStrong, arrObj.arrMutableCopy);
NSLog(@"值9:%@, %@, %@, %@, %@", arr1[], arrObj.arrStrong[], arrObj.arrCopy[], arrObj.arrMutableStrong[], arrObj.arrMutableCopy[]);
/*
指针9:0x7fd8e8e04870, 0x7fd8e8e04870, 0x7fd8e8e04870, 0x7fd8e8e04870, 0x7fd8e8e04870
值9:twotwo2, twotwo2, twotwo2, twotwo2, twotwo2
*/ arr1 = @[@"TEST哈"];
NSLog(@"指针10:%p, %p, %p, %p, %p", arr1, arrObj.arrStrong, arrObj.arrCopy, arrObj.arrMutableStrong, arrObj.arrMutableCopy);
NSLog(@"值10:%@, %@, %@, %@, %@", arr1[], arrObj.arrStrong[], arrObj.arrCopy[], arrObj.arrMutableStrong[], arrObj.arrMutableCopy[]);
/*
指针10:0x7fd8e8e0cda0, 0x7fd8e8e04870, 0x7fd8e8e04870, 0x7fd8e8e04870, 0x7fd8e8e04870
值10:TEST哈, twotwo2, twotwo2, twotwo2, twotwo2
*/ NSMutableArray *arr6 = [NSMutableArray arrayWithObjects:@"风清扬", nil];
//赋值可变数组
arrObj.arrStrong = arr6; //指针拷贝
arrObj.arrCopy = arr6; //内容拷贝
arrObj.arrMutableStrong = arr6; //指针拷贝
arrObj.arrMutableCopy = arr6; //内容拷贝 NSLog(@"指针11:%p, %p, %p, %p, %p", arr6, arrObj.arrStrong, arrObj.arrCopy, arrObj.arrMutableStrong, arrObj.arrMutableCopy);
NSLog(@"值11:%@, %@, %@, %@, %@", arr6[], arrObj.arrStrong[], arrObj.arrCopy[], arrObj.arrMutableStrong[], arrObj.arrMutableCopy[]);
/*
指针11:0x7fd8e8f1cce0, 0x7fd8e8f1cce0, 0x7fd8e8c14480, 0x7fd8e8f1cce0, 0x7fd8e8c14f00
值11:风清扬, 风清扬, 风清扬, 风清扬, 风清扬
*/ arr6[] = @"哈宝";
NSLog(@"指针12:%p, %p, %p, %p, %p", arr6, arrObj.arrStrong, arrObj.arrCopy, arrObj.arrMutableStrong, arrObj.arrMutableCopy);
NSLog(@"值12:%@, %@, %@, %@, %@", arr6[], arrObj.arrStrong[], arrObj.arrCopy[], arrObj.arrMutableStrong[], arrObj.arrMutableCopy[]);
/*
指针12:0x7fd8e8f1cce0, 0x7fd8e8f1cce0, 0x7fd8e8c14480, 0x7fd8e8f1cce0, 0x7fd8e8c14f00
值12:哈宝, 哈宝, 风清扬, 哈宝, 风清扬
*/
结论:1)、不可变数组直接赋值给strong、copy修饰的数组属性,都是指针拷贝
2)、可变数组直接赋值给strong修饰的可变或不可变数组属性,都是指针拷贝;赋值给copy修饰的可变或不可变数组属性,都是内容拷贝
效果和字符串属性的效果是一样的!
2、数组调用copy、mutableCopy方法给数组赋值
NSArray *arr1 = [[NSArray alloc] initWithObjects:@"onone", nil]; NSArray *arr2 = [[NSArray alloc] initWithObjects:@"onone", nil];
NSArray *arr3 = [[NSArray alloc] initWithObjects:@"onone", nil]; NSMutableArray *arr4 = [[NSMutableArray alloc] initWithObjects:@"onone", nil];
NSMutableArray *arr5 = [[NSMutableArray alloc] initWithObjects:@"onone", nil]; NSLog(@"指针0:%p, %p, %p, %p, %p", arr1, arr2, arr3, arr4, arr5);
/*
指针0:0x7fd8e8db25b0, 0x7fd8e8d06540, 0x7fd8e8d132b0, 0x7fd8e8db1bc0, 0x7fd8e8db2620
*/ arr2 = [arr1 copy]; //指针拷贝
arr3 = [arr1 mutableCopy]; //内容拷贝
arr4 = [arr1 copy]; //指针拷贝
arr5 = [arr1 mutableCopy]; //内容拷贝
NSLog(@"指针1:%p, %p, %p, %p, %p", arr1, arr2, arr3, arr4, arr5);
NSLog(@"值1:%@, %@, %@, %@, %@", arr1[], arr2[], arr3[], arr4[], arr5[]);
/*
指针1:0x7fd8e8db25b0, 0x7fd8e8db25b0, 0x7fd8e8daff70, 0x7fd8e8db25b0, 0x7fd8e8db1bc0
值1:onone, onone, onone, onone, onone
*/ arr1 = @[@"twotwo"];
NSLog(@"指针2:%p, %p, %p, %p, %p", arr1, arr2, arr3, arr4, arr5);
NSLog(@"值2:%@, %@, %@, %@, %@", arr1[], arr2[], arr3[], arr4[], arr5[]);
/*
指针2:0x7fd8e8f036e0, 0x7fd8e8db25b0, 0x7fd8e8daff70, 0x7fd8e8db25b0, 0x7fd8e8db1bc0
值2:twotwo, onone, onone, onone, onone
*/ NSMutableArray *arr6 = [NSMutableArray arrayWithObjects:@"three", nil];
arr2 = [arr6 copy]; //内容拷贝
arr3 = [arr6 mutableCopy]; //内容拷贝
arr4 = [arr6 copy]; //内容拷贝
arr5 = [arr6 mutableCopy]; //内容拷贝
NSLog(@"指针3:%p, %p, %p, %p, %p", arr6, arr2, arr3, arr4, arr5);
NSLog(@"值3:%@, %@, %@, %@, %@", arr6[], arr2[], arr3[], arr4[], arr5[]);
/*
指针3:0x7fd8e8f1cce0, 0x7fd8e8f06d90, 0x7fd8e8f0c7c0, 0x7fd8e8f0c7f0, 0x7fd8e8f03150
值3:three, three, three, three, three
*/
arr6[] = @"大侠";
NSLog(@"指针4:%p, %p, %p, %p, %p", arr6, arr2, arr3, arr4, arr5);
NSLog(@"值4:%@, %@, %@, %@, %@", arr6[], arr2[], arr3[], arr4[], arr5[]);
/*
指针4:0x7fd8e8f1cce0, 0x7fd8e8f06d90, 0x7fd8e8f0c7c0, 0x7fd8e8f0c7f0, 0x7fd8e8f03150
值4:大侠, three, three, three, three
*/
结论:1)、不可变数组调用copy赋值给可变数组或不可变数组都是指针拷贝;不可变数组调用mutableCopy赋值给可变数组或不可变数组都是内容拷贝
2)、可变数组调用copy或mutableCopy赋值给可变数组或不可变数组,都是内容拷贝
效果和字符串也是一样!
3、数组调用copy、mutableCopy方法给数组属性赋值
TestArr *obj = [[TestArr alloc] init];
NSArray *arr = [NSArray arrayWithObjects:@"one哦", nil];
//都是指针拷贝
obj.arrStrong = [arr copy];
obj.arrCopy = [arr copy];
obj.arrMutableStrong = [arr copy];
obj.arrMutableCopy = [arr copy];
NSLog(@"指针1:%p, %p, %p, %p, %p", arr, obj.arrStrong, obj.arrCopy, obj.arrMutableStrong, obj.arrMutableCopy);
NSLog(@"值1:%@, %@, %@, %@, %@", arr[], obj.arrStrong[], obj.arrCopy[], obj.arrMutableStrong[], obj.arrMutableCopy[]);
/*
指针1:0x7fbf18c2a2b0, 0x7fbf18c2a2b0, 0x7fbf18c2a2b0, 0x7fbf18c2a2b0, 0x7fbf18c2a2b0
值1:one哦, one哦, one哦, one哦, one哦
*/ //都是内容拷贝
arr = [NSArray arrayWithObjects:@"two他", nil];
obj.arrStrong = [arr mutableCopy];
obj.arrCopy = [arr mutableCopy];
obj.arrMutableStrong = [arr mutableCopy];
obj.arrMutableCopy = [arr mutableCopy];
NSLog(@"指针2:%p, %p, %p, %p, %p", arr, obj.arrStrong, obj.arrCopy, obj.arrMutableStrong, obj.arrMutableCopy);
NSLog(@"值2:%@, %@, %@, %@, %@", arr[], obj.arrStrong[], obj.arrCopy[], obj.arrMutableStrong[], obj.arrMutableCopy[]);
/*
指针2:0x7fbf18f0ec40, 0x7fbf18d09e80, 0x7fbf18d36540, 0x7fbf18d09eb0, 0x7fbf18d014b0
值2:two他, two他, two他, two他, two他
*/ //可变数组
//都是内容拷贝
NSMutableArray *arr2 = [NSMutableArray arrayWithObjects:@"th叁", nil];
obj.arrStrong = [arr2 copy];
obj.arrCopy = [arr2 copy];
obj.arrMutableStrong = [arr2 copy];
obj.arrMutableCopy = [arr2 copy];
NSLog(@"指针3:%p, %p, %p, %p, %p", arr2, obj.arrStrong, obj.arrCopy, obj.arrMutableStrong, obj.arrMutableCopy);
NSLog(@"值3:%@, %@, %@, %@, %@", arr2[], obj.arrStrong[], obj.arrCopy[], obj.arrMutableStrong[], obj.arrMutableCopy[]);
/*
指针3:0x7fbf18f6c420, 0x7fbf18f693a0, 0x7fbf18f00500, 0x7fbf18f63c60, 0x7fbf18f01660
值3:th叁, th叁, th叁, th叁, th叁
*/ arr2[] = @"司ss";
NSLog(@"指针3_2:%p, %p, %p, %p, %p", arr2, obj.arrStrong, obj.arrCopy, obj.arrMutableStrong, obj.arrMutableCopy);
NSLog(@"值3_2:%@, %@, %@, %@, %@", arr2[], obj.arrStrong[], obj.arrCopy[], obj.arrMutableStrong[], obj.arrMutableCopy[]);
/*
值3_2:司ss, th叁, th叁, th叁, th叁
*/ //都是内容拷贝
obj.arrStrong = [arr2 mutableCopy];
obj.arrCopy = [arr2 mutableCopy];
obj.arrMutableStrong = [arr2 mutableCopy];
obj.arrMutableCopy = [arr2 mutableCopy];
NSLog(@"指针4:%p, %p, %p, %p, %p", arr2, obj.arrStrong, obj.arrCopy, obj.arrMutableStrong, obj.arrMutableCopy);
NSLog(@"值4:%@, %@, %@, %@, %@", arr2[], obj.arrStrong[], obj.arrCopy[], obj.arrMutableStrong[], obj.arrMutableCopy[]);
/*
指针4:0x7fbf18f6c420, 0x7fbf18f620e0, 0x7fbf18f693a0, 0x7fbf18f6c9a0, 0x7fbf18d014b0
值4:司ss, 司ss, 司ss, 司ss, 司ss
*/ arr2[]=@"Ni打野";
NSLog(@"指针5:%p, %p, %p, %p, %p", arr2, obj.arrStrong, obj.arrCopy, obj.arrMutableStrong, obj.arrMutableCopy);
NSLog(@"值5:%@, %@, %@, %@, %@", arr2[], obj.arrStrong[], obj.arrCopy[], obj.arrMutableStrong[], obj.arrMutableCopy[]);
/*
值5:Ni打野, 司ss, 司ss, 司ss, 司ss
*/
结论:1)、不可变数组调用copy给strong、copy修饰的数组属性赋值,都是指针拷贝;
2)、 不可变数组调用mutableCopy, 和可变数组调用copy, 和可变数组调用mutableCopy方法给strong、copy修饰的数组属性赋值,都是内容拷贝
和字符串效果也是一样
--------------- 三 end ---------------
原文链接:http://www.cnblogs.com/tandaxia/p/4475410.html
iOS之属性修饰符 retain、strong和copy区别测试的更多相关文章
- iOS 属性修饰符记录 --不定时更新
重新审视了一下OC在属性修饰符,特意记录一下来.以后不定时更新 > retain:只有在非ARC下才会有效,所有如果在ARC下使用了retain修饰也白搭 如以下的data属性用retain修饰 ...
- 关于@property()的那些属性及ARC简介【nonatomic,atomic,assign,retain,strong,weak,copy。】
@property()常用的属性有:nonatomic,atomic,assign,retain,strong,weak,copy. 其中atomic和nonatomic用来决定编译器生成的gette ...
- 参数修饰符ref,out ,params的区别
参数修饰符ref,out ,params的区别 C#中有三个关键字-ref,out ,params,可是这三个之间的区别你都明白了吗? 那么我们就来认识一下参数修饰符ref,out ,params吧, ...
- iOS的属性声明:retain和strong的区别
声明属性时用strong或者retain效果是一样的(貌似更多开发者更倾向于用strong).不过在声明Block时,使用strong和retain会有截然不同的效果.strong会等于copy,而r ...
- UE4C++定义属性修饰符总结
1.BlueprintAssignable 暴露该属性来在蓝图中进行赋值,用于绑定多播委托 2.BlueprintCallable 用于从蓝图中调用C++原生函数 3.BlueprintReadO ...
- iOS学习——属性引用self.xx与_xx的区别
在iOS开发过程中,我们用@proprety声明一个属性后,在代码中我们可以用self.xx与_xx来获取到这个属性.但是一直有一个疑惑,那就是这两个之间有什么区别呢?最初我一直觉得这两个之间没什么区 ...
- IOS 类的属性修饰符atomic
在声明一个类的属性时,默认这个属性会被修饰atomic,意思是原子性访问的. nonatomic和atomic修饰的属性,在自己没有重写setter和getter的时候才会发生作用,其主要的作用可以理 ...
- ios学习路线—Objective-C(属性修饰符)
readonly: 此标记说明属性是只读的,默认的标记是读写,如果你指定了只读,在@implementation中只需要一个读取器.或者如果你使用@synthesize关键字,也是有读取器方法被解析. ...
- iOS 实例变量修饰符
@public 可以在其他类中访问被@public修饰的成员变量 可以在本类中访问被@public修饰的成员变量 可以在子类中访问fl中被@public修饰的成员变量 @private 不可以在其他类 ...
随机推荐
- Windows Azure Web Site (16) Azure Web Site HTTPS
<Windows Azure Platform 系列文章目录> 我们在使用微软云Azure Web App的时候,会使用微软的二级域名:http://xxx.chinacloudsites ...
- Git学习笔记(二)
一.创建远程仓库(GitHub) 1.GitHub网站地址:https://github.com/,这个网站就是提供Git仓库托管服务的,所以,只要注册一个GitHub账号,就可以免费获得Git远程仓 ...
- 【原创】Kakfa serializer包源代码分析
这个包很简单,只有两个scala文件: decoder和encoder,就是提供序列化/反序列化的服务.我们一个一个说. 一.Decoder.scala 首先定义了一个trait: Decoder[T ...
- 使用nodejs+express(4.x+)实现文件上传
最简单的做法是通过“connect-multiparty”中间件实现上传. 通过在项目中npm install connect-multiparty进行安装. 用法: var multipart = ...
- 把DataTable转换为泛型List<T>或是JSON
在开发ASP.NET Web API或ASP.NET MVC时,我们从数据库得到的数据往往是DataSet或是DataTable.为了能让前端JQuery能方便使用至这些数据,我们需要把这些数据转换为 ...
- 背水一战 Windows 10 (10) - 资源: StaticResource, ThemeResource
[源码下载] 背水一战 Windows 10 (10) - 资源: StaticResource, ThemeResource 作者:webabcd 介绍背水一战 Windows 10 之 资源 St ...
- 【Java每日一题】20161128
package Nov2016; import java.util.ArrayList; import java.util.List; public class Ques1128 { public s ...
- js中获取URL中指定的查询字符串
js中获取URL中指定的搜索字符串,主要利用location对象实现,废话少说,上代码. function getSearchString(key) { // 获取URL中?之后的字符 var str ...
- Android自定义surfaceView显示多张图片
我自定义了一个surfaceview,我在上面绘制多张图片,让它能够上下方滚显示图片,但是onMeasure()方法在重写的时候遇到了问题,不知道如何设置它的高度, public class MySu ...
- Scalaz(4)- typeclass:标准类型-Equal,Order,Show,Enum
Scalaz是由一堆的typeclass组成.每一个typeclass具备自己特殊的功能.用户可以通过随意多态(ad-hoc polymorphism)把这些功能施用在自己定义的类型上.scala这个 ...