iOS之属性修饰符 retain、strong和copy区别测试

时不时会有点迷惑属性修饰符retain、strong、copy三者之间的区别,还是把测试过程记录下来好一点!

1、属性修饰符结论

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

测试代码:

 1 Person *per = [[Person alloc] init];
 2     NSString *name = @"原XM";
 3     [per setStrRetain:name];
 4     [per setStrStrong:name];
 5     per.strCopy = name;
 6     
 7     NSMutableString *name2 = [[NSMutableString alloc] initWithString:@"原XM2"];
 8     per.strMutableRetain = name2;
 9     per.strMutableStrong = name2;
10     per.strMutableCopy = name2;
11     
12     NSLog(@"------ 改变之前打印 ------");
13     NSLog(@"per.strRetain: %@, ", per.strRetain);
14     NSLog(@"per.strStrong: %@", per.strStrong);
15     NSLog(@"per.strCopy: %@", per.strCopy);
16     NSLog(@"per.strMutableRetain: %@", per.strMutableRetain);
17     NSLog(@"per.strMutableStrong: %@", per.strMutableStrong);
18     NSLog(@"per.strMutableCopy: %@", per.strMutableCopy);
19     
20     name = @"新XM";
21     [name2 appendString:@"新XM2"];
22     
23     NSLog(@"------- 改变之后打印 -------");
24     NSLog(@"per.strRetain: %@", per.strRetain);
25     NSLog(@"per.strStrong: %@", per.strStrong);
26     NSLog(@"per.strCopy: %@", per.strCopy);
27     NSLog(@"per.strMutableRetain: %@", per.strMutableRetain);
28     NSLog(@"per.strMutableStrong: %@", per.strMutableStrong);
29     NSLog(@"per.strMutableCopy: %@", per.strMutableCopy);
View Code

打印日志:

 ------ 改变之前打印 ------
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
View Code

结论:

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", 8];
    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神
     */
View Code

再次描述下结论:

给字符串属性赋值不可变字符串,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吧
     */
View Code

结论: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
View Code

结论:1)、不可变字符串调用copystrong, copy修饰的属性赋值,都是指针拷贝;

         2)、不可变字符串调用mutableCopy, 和可变字符串调用copy,和可变字符串调用mutableCopy方法给strongcopy修饰的属性赋值,都是内容拷贝

 

所以说属性修饰符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[0], arrObj.arrStrong[0], arrObj.arrCopy[0], arrObj.arrMutableStrong[0], arrObj.arrMutableCopy[0]);
    /*
     指针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[0], arrObj.arrStrong[0], arrObj.arrCopy[0], arrObj.arrMutableStrong[0], arrObj.arrMutableCopy[0]);
    /*
     指针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[0], arrObj.arrStrong[0], arrObj.arrCopy[0], arrObj.arrMutableStrong[0], arrObj.arrMutableCopy[0]);
    /*
     指针11:0x7fd8e8f1cce0, 0x7fd8e8f1cce0, 0x7fd8e8c14480, 0x7fd8e8f1cce0, 0x7fd8e8c14f00
     值11:风清扬, 风清扬, 风清扬, 风清扬, 风清扬
     */

    arr6[0] = @"哈宝";
    NSLog(@"指针12:%p, %p, %p, %p, %p", arr6, arrObj.arrStrong, arrObj.arrCopy, arrObj.arrMutableStrong, arrObj.arrMutableCopy);
    NSLog(@"值12:%@, %@, %@, %@, %@", arr6[0], arrObj.arrStrong[0], arrObj.arrCopy[0], arrObj.arrMutableStrong[0], arrObj.arrMutableCopy[0]);
    /*
     指针12:0x7fd8e8f1cce0, 0x7fd8e8f1cce0, 0x7fd8e8c14480, 0x7fd8e8f1cce0, 0x7fd8e8c14f00
     值12:哈宝, 哈宝, 风清扬, 哈宝, 风清扬
     */
View Code

结论:1)、不可变数组直接赋值给strongcopy修饰的数组属性,都是指针拷贝

         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[0], arr2[0], arr3[0], arr4[0], arr5[0]);
    /*
     指针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[0], arr2[0], arr3[0], arr4[0], arr5[0]);
    /*
     指针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[0], arr2[0], arr3[0], arr4[0], arr5[0]);
    /*
     指针3:0x7fd8e8f1cce0, 0x7fd8e8f06d90, 0x7fd8e8f0c7c0, 0x7fd8e8f0c7f0, 0x7fd8e8f03150
     值3:three, three, three, three, three
     */
    arr6[0] = @"大侠";
    NSLog(@"指针4:%p, %p, %p, %p, %p", arr6, arr2, arr3, arr4, arr5);
    NSLog(@"值4:%@, %@, %@, %@, %@", arr6[0], arr2[0], arr3[0], arr4[0], arr5[0]);
    /*
     指针4:0x7fd8e8f1cce0, 0x7fd8e8f06d90, 0x7fd8e8f0c7c0, 0x7fd8e8f0c7f0, 0x7fd8e8f03150
     值4:大侠, three, three, three, three
     */
View Code

结论:1)、不可变数组调用copy赋值给可变数组或不可变数组都是指针拷贝;不可变数组调用mutableCopy赋值给可变数组或不可变数组都是内容拷贝

         2)、可变数组调用copymutableCopy赋值给可变数组或不可变数组,都是内容拷贝

      效果和字符串也是一样!

 

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[0], obj.arrStrong[0], obj.arrCopy[0], obj.arrMutableStrong[0], obj.arrMutableCopy[0]);
    /*
     指针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[0], obj.arrStrong[0], obj.arrCopy[0], obj.arrMutableStrong[0], obj.arrMutableCopy[0]);
    /*
     指针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[0], obj.arrStrong[0], obj.arrCopy[0], obj.arrMutableStrong[0], obj.arrMutableCopy[0]);
    /*
     指针3:0x7fbf18f6c420, 0x7fbf18f693a0, 0x7fbf18f00500, 0x7fbf18f63c60, 0x7fbf18f01660
     值3:th叁, th叁, th叁, th叁, th叁
     */
    
    
    arr2[0] = @"司ss";
    NSLog(@"指针3_2:%p, %p, %p, %p, %p", arr2, obj.arrStrong, obj.arrCopy, obj.arrMutableStrong, obj.arrMutableCopy);
    NSLog(@"值3_2:%@, %@, %@, %@, %@", arr2[0], obj.arrStrong[0], obj.arrCopy[0], obj.arrMutableStrong[0], obj.arrMutableCopy[0]);
    /*
     值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[0], obj.arrStrong[0], obj.arrCopy[0], obj.arrMutableStrong[0], obj.arrMutableCopy[0]);
    /*
     指针4:0x7fbf18f6c420, 0x7fbf18f620e0, 0x7fbf18f693a0, 0x7fbf18f6c9a0, 0x7fbf18d014b0
     值4:司ss, 司ss, 司ss, 司ss, 司ss
     */
    
    arr2[0]=@"Ni打野";
    NSLog(@"指针5:%p, %p, %p, %p, %p", arr2, obj.arrStrong, obj.arrCopy, obj.arrMutableStrong, obj.arrMutableCopy);
    NSLog(@"值5:%@, %@, %@, %@, %@", arr2[0], obj.arrStrong[0], obj.arrCopy[0], obj.arrMutableStrong[0], obj.arrMutableCopy[0]);
    /*
     值5:Ni打野, 司ss, 司ss, 司ss, 司ss
     */
View Code

结论:1)、不可变数组调用copystrongcopy修饰的数组属性赋值,都是指针拷贝;

     2)、 不可变数组调用mutableCopy, 和可变数组调用copy, 和可变数组调用mutableCopy方法给strongcopy修饰的数组属性赋值,都是内容拷贝

    和字符串效果也是一样

---------------  三  end  ---------------

 

原文链接:http://www.cnblogs.com/tandaxia/p/4475410.html 

 

posted @ 2015-05-04 09:37  谈晓鸣  阅读(2628)  评论(1编辑  收藏  举报