• 从copy和mutableCopy谈起(http://www.cocoachina.com/ios/20151202/14520.html)


    从copy和mutableCopy谈起

    copy和mutableCopy

    NSObject类有两个跟拷贝相关的方法——copy和mutableCopy。这两个方法都是返回一个id类型的对象,那么这两者之间有什么区别呢?根据官方文档解释,copy方法,返回copyWithZone方法返回的对象(Returns the object returned by copyWithZone:)。而mutableCopy方法,返回mutableCopyWithZone方法返回的对象(Returns the object returned by mutableCopyWithZone:)。读起来有点绕,一言以蔽之,调用copy就是调用copyWithZone,调用mutableCopy就是调用mutableCopyWithZone。还是不够清楚!!!接下来我们以NSString为例子,来说明copy和mutableCopy的区别。

    NSString对象调用copy和mutableCopy

    那么对NSString对象调用copy和mutableCopy究竟有什么效果呢? 在实验之前先定义如下通用宏

    1
    2
    3
    4
    5
    6
    //打印方法名
    #define LOG_METHOD_NAME {NSLog(@"%@", NSStringFromSelector(_cmd));}
    //打印对象的类名,以及对象本身的地址
    #define LOG_OBJ_ADDRESS(obj) {NSLog(@"%@ : %p",NSStringFromClass([obj class]), obj);}
    //打印空行
    #define LOG_END {NSLog(@"%@", @" ");}

    测试代码如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
     1 - (void)stringCopyTest
     2 {
     3     LOG_METHOD_NAME
     4     NSString *str = @"Hello";
     5     LOG_OBJ_ADDRESS(str);
     6     NSString *cpStr = [str copy];
     7     LOG_OBJ_ADDRESS(cpStr);
     8     NSMutableString *mutCpStr = [str mutableCopy];
     9     LOG_OBJ_ADDRESS(mutCpStr);
    10     LOG_END
    11 }

    输出结果如下:

    1
    2
    3
    4
    5
    1 2015-11-22 19:58:05.738 CopyTest[17230:1045019] stringCopyTest
    2 2015-11-22 19:58:05.738 CopyTest[17230:1045019] __NSCFConstantString : 0x100fee0b0
    3 2015-11-22 19:58:05.738 CopyTest[17230:1045019] __NSCFConstantString : 0x100fee0b0
    4 2015-11-22 19:58:05.738 CopyTest[17230:1045019] __NSCFString : 0x7fabbaa00190
    5 2015-11-22 19:58:05.738 CopyTest[17230:1045019]

    由以上输出可知,对一个NSString对象调用copy返回的还是该对象本身,因为str的地址和cpStr的地址是同一个。而调用mutableCopy,返回的是一个NSMutableString对象(注:__NSCFConstantString是常量串即NSString,而__NSCFString是可变串即NSMutableString)。

    NSMutableString对象调用copy和mutableCopy

    同理,引申到NSMutableString对象呢? 测试代码如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
     1 - (void)mutableStringCopyTest
     2 {
     3     LOG_METHOD_NAME
     4     NSMutableString *mutStr = [@"OC" mutableCopy];
     5     LOG_OBJ_ADDRESS(mutStr);
     6     NSMutableString *cpMutStr = [mutStr copy];
     7     LOG_OBJ_ADDRESS(cpMutStr);
     8     NSMutableString *mutCpMutStr = [mutStr mutableCopy];
     9     LOG_OBJ_ADDRESS(mutCpMutStr);
    10     LOG_END
    11 }

    输出结果如下:

    1
    2
    3
    4
    5
    1 2015-11-22 19:58:05.738 CopyTest[17230:1045019] mutableStringCopyTest
    2 2015-11-22 19:58:05.738 CopyTest[17230:1045019] __NSCFString : 0x7fabba9012c0
    3 2015-11-22 19:58:05.739 CopyTest[17230:1045019] NSTaggedPointerString : 0xa0000000000434f2
    4 2015-11-22 19:58:05.739 CopyTest[17230:1045019] __NSCFString : 0x7fabb840a860
    5 2015-11-22 19:58:05.739 CopyTest[17230:1045019]

    由以上输出可知,对一个NSMutableString对象调用copy返回的是一个NSTaggedPointerString对象,该对象可认为是一个常量串。而调用mutableCopy返回的是另外一个可变对象__NSCFString,即NSMutableString(原NSMutableString对象的地址是0x7fabba9012c0,新NSMutableString对象地址是0x7fabb840a860)。

    针对NSArray、NSDictionary、NSSet等具有Mutable版本的类进行试验出现跟NSString类似的现象,不一一列举,有兴趣可以自己去试验。

    copy和mutableCopy调用小结

    • 针对不可变对象调用copy返回该对象本身,调用mutableCopy返回一个可变对象(新的);

    • 针对可变对象调用copy返回一个不可变对象(新的),调用mutableCopy返回另外一个可变对象(新的)。

    class copy mutableCopy
    不可变(如,NSString) 返回本身(相当于retain一次) 创建新的可变对象(如,创建一个NSMutableString对象,地址跟原对象不同)
    可变(如,NSMutableString) 创建新的不可变对象(如,创建一个NSTaggedPointerString对象,地址跟原对象不同 ) 创建新的可变对象(如,创建一个NSMutableString对象,地址跟原对象不同 )

    再进一步从是否新建返回对象,返回对象是否可变两个角度总结如下:

    • 只有不可变的copy是retain一次,其他都是创建一个新对象;

    • copy返回的是不可变对象,mutableCopy返回的是可变对象。

    属性copy还是strong

    假设有两个id类型的属性如下:

    1
    2
    1 @property (nonatomic, copy) id cpID;
    2 @property (nonatomic, strong) id stID;

    那么编译器把以上两属性分别实现为:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
     1 - (void)setCpID:(id)cpID {
     2     _cpID = [cpID copy];
     3 }
     
     5 - (id)cpID {
     6     return _cpID;
     7 }
     
     9 - (void)setStID:(id)stID {
    10     _stID = stID;
    11 }
    12 
    13 - (id)stID {
    14     return _stID;
    15 }

    从以上实现可以看出,strong和copy的属性主要是set方法有区别,strong的set是直接设置指定值,而copy的set是设置指定值的copy版本。接下来探索一下NSString、NSMutableString的copy和strong属性。

    NSString属性copy还是strong

    测试代码如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
     1 @property (nonatomic, copy) NSString *cpStr;
     2 @property (nonatomic, strong) NSString *stStr;
     
     4 - (void)stringPropertyTest
     5 {
     6     LOG_METHOD_NAME
     7     NSMutableString *mutStr = [@"123" mutableCopy];
     8     LOG_OBJ_ADDRESS(mutStr);
     9     self.cpStr = mutStr;
    10     LOG_OBJ_ADDRESS(self.cpStr);
    11     self.stStr = mutStr;
    12     LOG_OBJ_ADDRESS(self.stStr);
    13     
    14     NSLog(@"修改前");
    15     NSLog(@"mutStr:%@", mutStr);
    16     NSLog(@"copy:%@", self.cpStr);
    17     NSLog(@"strong:%@", self.stStr);
    18     
    19     [mutStr appendString:@"456"];
    20     NSLog(@"修改后");
    21     NSLog(@"mutStr:%@", mutStr);
    22     NSLog(@"copy:%@", self.cpStr);
    23     NSLog(@"strong:%@", self.stStr);
    24     
    25     LOG_END
    26 }

    输出结果如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     1 2015-11-22 21:11:29.458 CopyTest[17977:1317284] stringPropertyTest
     2 2015-11-22 21:11:29.458 CopyTest[17977:1317284] __NSCFString : 0x7fad23409b80
     3 2015-11-22 21:11:29.458 CopyTest[17977:1317284] NSTaggedPointerString : 0xa000000003332313
     4 2015-11-22 21:11:29.458 CopyTest[17977:1317284] __NSCFString : 0x7fad23409b80
     5 2015-11-22 21:11:29.458 CopyTest[17977:1317284] 修改前
     6 2015-11-22 21:11:29.458 CopyTest[17977:1317284] mutStr:123
     7 2015-11-22 21:11:29.458 CopyTest[17977:1317284] copy:123
     8 2015-11-22 21:11:29.458 CopyTest[17977:1317284] strong:123
     9 2015-11-22 21:11:29.458 CopyTest[17977:1317284] 修改后
    10 2015-11-22 21:11:29.458 CopyTest[17977:1317284] mutStr:123456
    11 2015-11-22 21:11:29.459 CopyTest[17977:1317284] copy:123
    12 2015-11-22 21:11:29.459 CopyTest[17977:1317284] strong:123456
    13 2015-11-22 21:11:29.459 CopyTest[17977:1317284]

    由以上输出可知,假设两个NSString属性实际上指向的都是一个NSMutableString对象,那么在原NSMutableString对象修改后,strong版本的NSString属性跟着修改,而copy版本属性保持原状。self.cpStr实际上是一个NSTaggedPointerString对象,该对象正是NSMutableString对象执行copy的返回值。

    NSMutableString属性copy还是strong

    测试代码如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
     1 @property (nonatomic, copy) NSMutableString *cpMutStr;
     2 @property (nonatomic, strong) NSMutableString *stMutStr;
     
     4 - (void)mutableStringPropertyTest
     5 {
     6     LOG_METHOD_NAME
     7     NSMutableString *mutStr = [@"123" mutableCopy];
     8     LOG_OBJ_ADDRESS(mutStr);
     9     self.cpMutStr = mutStr;
    10     LOG_OBJ_ADDRESS(self.cpMutStr);
    11     self.stMutStr = mutStr;
    12     LOG_OBJ_ADDRESS(self.stMutStr);
    13     
    14     NSLog(@"修改前");
    15     NSLog(@"mutStr:%@", mutStr);
    16     NSLog(@"copy:%@", self.cpMutStr);
    17     NSLog(@"strong:%@", self.stMutStr);
    18     
    19     [mutStr appendString:@"456"];
    20     NSLog(@"修改后");
    21     NSLog(@"mutStr:%@", mutStr);
    22     NSLog(@"copy:%@", self.cpMutStr);
    23     NSLog(@"strong:%@", self.stMutStr);
    24     LOG_END
    25 }

    输出结果如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     1 2015-11-22 21:48:21.774 CopyTest[18508:1539502] mutableStringPropertyTest
     2 2015-11-22 21:48:21.774 CopyTest[18508:1539502] __NSCFString : 0x7f806879b620
     3 2015-11-22 21:48:21.774 CopyTest[18508:1539502] NSTaggedPointerString : 0xa000000003332313
     4 2015-11-22 21:48:21.774 CopyTest[18508:1539502] __NSCFString : 0x7f806879b620
     5 2015-11-22 21:48:21.774 CopyTest[18508:1539502] 修改前
     6 2015-11-22 21:48:21.774 CopyTest[18508:1539502] mutStr:123
     7 2015-11-22 21:48:21.774 CopyTest[18508:1539502] copy:123
     8 2015-11-22 21:48:21.774 CopyTest[18508:1539502] strong:123
     9 2015-11-22 21:48:21.775 CopyTest[18508:1539502] 修改后
    10 2015-11-22 21:48:21.775 CopyTest[18508:1539502] mutStr:123456
    11 2015-11-22 21:48:21.775 CopyTest[18508:1539502] copy:123
    12 2015-11-22 21:48:21.775 CopyTest[18508:1539502] strong:123456
    13 2015-11-22 21:48:21.775 CopyTest[18508:1539502]

    看起来没啥问题,strong版本的属性跟随原对象的变化而变化,copy版本的属性不变。但是,假设调用

    1
    1 [self.cpMutStr appendString:@"789"];

    程序会崩溃。崩溃信息如下:

    1
    2
    1 2015-11-22 21:51:37.282 CopyTest[18542:1545579] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSTaggedPointerString appendString:]: unrecognized selector sent to instance 0xa000000003332313'
    2 *** First throw call stack:

    原因很明显,是朝NSTaggedPointerString对象发了一个它不能识别的selector。原因是copy版本的NSMutableString属性本质上不是一个NSMutableString对象,而是一个NSTaggedPointerString对象,它是一个不可变对象。该对象是NSMutableString对象执行copy得来的,还记得我们上一节的结论吗?对一个对象执行copy得到的用于是一个不可变的对象。

    针对NSArray、NSDictionary、NSSet等具有Mutable版本的类进行试验出现跟NSString类似的现象。

    结论

    • 不可变类型属性,推荐使用copy,因为假设该对象实际上指向的是一个mutable的对象,mutable对象的改变不会导致该对象的改变;假设指向的不是mutable的对象,那么copy和strong是等价,都是执行一次retain。

    • 可变类型属性,不能使用copy,因为copy产生的对象是一个不可变对象,跟属性描述是冲突的。

     
  • 相关阅读:
    SPOJ GSS1 ~ 8解题报告 【完整版】
    题解 UVA1659 【帮助小罗拉 Help Little Laura】
    题解 UVA753 【UNIX插头 A Plug for UNIX】
    题解 P3740 【[HAOI2014]贴海报】
    对拍程序
    dut新生大礼包3
    1240C
    DISCO Presents Discovery Channel Code Contest 2020 Qual 题解
    1254C
    1285E
  • 原文地址:https://www.cnblogs.com/HypeCheng/p/5230147.html
Copyright © 2020-2023  润新知