1 copy和mutableCopy
NSObject類(lèi)有兩個(gè)跟拷貝相關(guān)的方法——copy和mutableCopy颊艳。這兩個(gè)方法都是返回一個(gè)id類(lèi)型的對(duì)象,那么這兩者之間有什么區(qū)別呢施符?根據(jù)官方文檔解釋?zhuān)琧opy方法卸察,返回copyWithZone方法返回的對(duì)象(Returns the object returned by copyWithZone:)。而mutableCopy方法驱负,返回mutableCopyWithZone方法返回的對(duì)象(Returns the object returned by mutableCopyWithZone:)。讀起來(lái)有點(diǎn)繞患雇,一言以蔽之跃脊,調(diào)用copy即調(diào)用copyWithZone,調(diào)用mutableCopy即調(diào)用mutableCopyWithZone庆亡。還是不夠清楚匾乓!接下來(lái)我們以NSString為例子捞稿,來(lái)說(shuō)明copy和mutableCopy的區(qū)別又谋。
1.1 NSString對(duì)象調(diào)用copy和mutableCopy
那么對(duì)NSString對(duì)象調(diào)用copy和mutableCopy究竟有什么效果呢?
在實(shí)驗(yàn)之前先定義如下通用宏:
//打印方法名
#define LOG_METHOD_NAME {NSLog(@"%@", NSStringFromSelector(_cmd));}
//打印對(duì)象的類(lèi)名娱局,以及對(duì)象本身的地址
#define LOG_OBJ_ADDRESS(obj) {NSLog(@"%@ : %p",NSStringFromClass([obj class]), obj);}
//打印空行
#define LOG_END {NSLog(@"%@", @" ");}
測(cè)試代碼如下:
- (void)stringCopyTest
{
LOG_METHOD_NAME
NSString *str = @"Hello";
LOG_OBJ_ADDRESS(str);
NSString *cpStr = [str copy];
LOG_OBJ_ADDRESS(cpStr);
NSMutableString *mutCpStr = [str mutableCopy];
LOG_OBJ_ADDRESS(mutCpStr);
LOG_END
}
輸出結(jié)果如下:
2015-11-22 19:58:05.738 CopyTest[17230:1045019] stringCopyTest
2015-11-22 19:58:05.738 CopyTest[17230:1045019] __NSCFConstantString : 0x100fee0b0
2015-11-22 19:58:05.738 CopyTest[17230:1045019] __NSCFConstantString : 0x100fee0b0
2015-11-22 19:58:05.738 CopyTest[17230:1045019] __NSCFString : 0x7fabbaa00190
2015-11-22 19:58:05.738 CopyTest[17230:1045019]
由以上輸出可知彰亥,對(duì)一個(gè)NSString對(duì)象調(diào)用copy返回的還是該對(duì)象本身,因?yàn)閟tr的地址和cpStr的地址是同一個(gè)衰齐。而調(diào)用mutableCopy任斋,返回的是一個(gè)NSMutableString對(duì)象(注:__NSCFConstantString是常量串即NSString,而__NSCFString是可變串即NSMutableString)耻涛。
1.2 NSMutableString對(duì)象調(diào)用copy和mutableCopy
同理废酷,引申到NSMutableString對(duì)象呢瘟檩?
測(cè)試代碼如下:
- (void)mutableStringCopyTest
{
LOG_METHOD_NAME
NSMutableString *mutStr = [@"OC" mutableCopy];
LOG_OBJ_ADDRESS(mutStr);
NSMutableString *cpMutStr = [mutStr copy];
LOG_OBJ_ADDRESS(cpMutStr);
NSMutableString *mutCpMutStr = [mutStr mutableCopy];
LOG_OBJ_ADDRESS(mutCpMutStr);
LOG_END
}
輸出結(jié)果如下:
2015-11-22 19:58:05.738 CopyTest[17230:1045019] mutableStringCopyTest
2015-11-22 19:58:05.738 CopyTest[17230:1045019] __NSCFString : 0x7fabba9012c0
2015-11-22 19:58:05.739 CopyTest[17230:1045019] NSTaggedPointerString : 0xa0000000000434f2
2015-11-22 19:58:05.739 CopyTest[17230:1045019] __NSCFString : 0x7fabb840a860
2015-11-22 19:58:05.739 CopyTest[17230:1045019]
由以上輸出可知,對(duì)一個(gè)NSMutableString對(duì)象調(diào)用copy返回的是一個(gè)NSTaggedPointerString對(duì)象澈蟆,該對(duì)象可認(rèn)為是一個(gè)常量串墨辛。而調(diào)用mutableCopy返回的是另外一個(gè)可變對(duì)象__NSCFString,即NSMutableString(原NSMutableString對(duì)象的地址是0x7fabba9012c0趴俘,新NSMutableString對(duì)象地址是0x7fabb840a860)睹簇。
針對(duì)NSArray、NSDictionary寥闪、NSSet等具有Mutable版本的類(lèi)進(jìn)行試驗(yàn)出現(xiàn)跟NSString類(lèi)似的現(xiàn)象太惠,不一一列舉,有興趣可以自己去試驗(yàn)疲憋。
1.3 copy和mutableCopy調(diào)用小結(jié)
- 針對(duì)不可變對(duì)象調(diào)用copy返回該對(duì)象本身凿渊,調(diào)用mutableCopy返回一個(gè)可變對(duì)象(新的);
- 針對(duì)可變對(duì)象調(diào)用copy返回一個(gè)不可變對(duì)象(新的)缚柳,調(diào)用mutableCopy返回另外一個(gè)可變對(duì)象(新的)嗽元。
class | copy | mutableCopy |
---|---|---|
不可變(如,NSString) | 返回本身(其他什么都不干) | 創(chuàng)建新的可變對(duì)象(如喂击,創(chuàng)建一個(gè)NSMutableString對(duì)象剂癌,地址跟原對(duì)象不同) |
可變(如,NSMutableString) | 創(chuàng)建新的不可變對(duì)象(如翰绊,創(chuàng)建一個(gè)NSTaggedPointerString對(duì)象佩谷,地址跟原對(duì)象不同 ) | 創(chuàng)建新的可變對(duì)象(如,創(chuàng)建一個(gè)NSMutableString對(duì)象监嗜,地址跟原對(duì)象不同 ) |
再?gòu)氖欠裥陆ǚ祷貙?duì)象以及返回對(duì)象是否可變兩個(gè)角度總結(jié)如下:
- 只有不可變的copy是返回本身(其他什么都不干)谐檀,其他都是創(chuàng)建一個(gè)新對(duì)象;
- copy返回的是不可變對(duì)象裁奇,mutableCopy返回的是可變對(duì)象桐猬。
2 屬性copy還是strong
假設(shè)有兩個(gè)id類(lèi)型的屬性如下:
@property (nonatomic, copy) id cpID;
@property (nonatomic, strong) id stID;
那么編譯器把以上兩屬性分別實(shí)現(xiàn)為:
- (void)setCpID:(id)cpID {
_cpID = [cpID copy];
}
- (id)cpID {
return _cpID;
}
- (void)setStID:(id)stID {
_stID = stID;
}
- (id)stID {
return _stID;
}
從以上實(shí)現(xiàn)可以看出,strong和copy的屬性主要是set方法有區(qū)別刽肠,strong的set是直接設(shè)置指定值溃肪,而copy的set是設(shè)置指定值的copy版本。接下來(lái)探索一下NSString音五、NSMutableString的copy和strong屬性惫撰。
2.1 NSString屬性copy還是strong
測(cè)試代碼如下:
@property (nonatomic, copy) NSString *cpStr;
@property (nonatomic, strong) NSString *stStr;
- (void)stringPropertyTest
{
LOG_METHOD_NAME
NSMutableString *mutStr = [@"123" mutableCopy];
LOG_OBJ_ADDRESS(mutStr);
self.cpStr = mutStr;
LOG_OBJ_ADDRESS(self.cpStr);
self.stStr = mutStr;
LOG_OBJ_ADDRESS(self.stStr);
NSLog(@"修改前");
NSLog(@"mutStr:%@", mutStr);
NSLog(@"copy:%@", self.cpStr);
NSLog(@"strong:%@", self.stStr);
[mutStr appendString:@"456"];
NSLog(@"修改后");
NSLog(@"mutStr:%@", mutStr);
NSLog(@"copy:%@", self.cpStr);
NSLog(@"strong:%@", self.stStr);
LOG_END
}
輸出結(jié)果如下:
2015-11-22 21:11:29.458 CopyTest[17977:1317284] stringPropertyTest
2015-11-22 21:11:29.458 CopyTest[17977:1317284] __NSCFString : 0x7fad23409b80
2015-11-22 21:11:29.458 CopyTest[17977:1317284] NSTaggedPointerString : 0xa000000003332313
2015-11-22 21:11:29.458 CopyTest[17977:1317284] __NSCFString : 0x7fad23409b80
2015-11-22 21:11:29.458 CopyTest[17977:1317284] 修改前
2015-11-22 21:11:29.458 CopyTest[17977:1317284] mutStr:123
2015-11-22 21:11:29.458 CopyTest[17977:1317284] copy:123
2015-11-22 21:11:29.458 CopyTest[17977:1317284] strong:123
2015-11-22 21:11:29.458 CopyTest[17977:1317284] 修改后
2015-11-22 21:11:29.458 CopyTest[17977:1317284] mutStr:123456
2015-11-22 21:11:29.459 CopyTest[17977:1317284] copy:123
2015-11-22 21:11:29.459 CopyTest[17977:1317284] strong:123456
2015-11-22 21:11:29.459 CopyTest[17977:1317284]
由以上輸出可知,假設(shè)兩個(gè)NSString屬性實(shí)際上指向的都是一個(gè)NSMutableString對(duì)象躺涝,那么在原NSMutableString對(duì)象修改后厨钻,strong版本的NSString屬性跟著修改,而copy版本屬性保持原狀。self.cpStr實(shí)際上是一個(gè)NSTaggedPointerString對(duì)象夯膀,該對(duì)象正是NSMutableString對(duì)象執(zhí)行copy的返回值诗充。
2.2 NSMutableString屬性copy還是strong
測(cè)試代碼如下:
@property (nonatomic, copy) NSMutableString *cpMutStr;
@property (nonatomic, strong) NSMutableString *stMutStr;
- (void)mutableStringPropertyTest
{
LOG_METHOD_NAME
NSMutableString *mutStr = [@"123" mutableCopy];
LOG_OBJ_ADDRESS(mutStr);
self.cpMutStr = mutStr;
LOG_OBJ_ADDRESS(self.cpMutStr);
self.stMutStr = mutStr;
LOG_OBJ_ADDRESS(self.stMutStr);
NSLog(@"修改前");
NSLog(@"mutStr:%@", mutStr);
NSLog(@"copy:%@", self.cpMutStr);
NSLog(@"strong:%@", self.stMutStr);
[mutStr appendString:@"456"];
NSLog(@"修改后");
NSLog(@"mutStr:%@", mutStr);
NSLog(@"copy:%@", self.cpMutStr);
NSLog(@"strong:%@", self.stMutStr);
LOG_END
}
輸出結(jié)果如下:
2015-11-22 21:48:21.774 CopyTest[18508:1539502] mutableStringPropertyTest
2015-11-22 21:48:21.774 CopyTest[18508:1539502] __NSCFString : 0x7f806879b620
2015-11-22 21:48:21.774 CopyTest[18508:1539502] NSTaggedPointerString : 0xa000000003332313
2015-11-22 21:48:21.774 CopyTest[18508:1539502] __NSCFString : 0x7f806879b620
2015-11-22 21:48:21.774 CopyTest[18508:1539502] 修改前
2015-11-22 21:48:21.774 CopyTest[18508:1539502] mutStr:123
2015-11-22 21:48:21.774 CopyTest[18508:1539502] copy:123
2015-11-22 21:48:21.774 CopyTest[18508:1539502] strong:123
2015-11-22 21:48:21.775 CopyTest[18508:1539502] 修改后
2015-11-22 21:48:21.775 CopyTest[18508:1539502] mutStr:123456
2015-11-22 21:48:21.775 CopyTest[18508:1539502] copy:123
2015-11-22 21:48:21.775 CopyTest[18508:1539502] strong:123456
2015-11-22 21:48:21.775 CopyTest[18508:1539502]
看起來(lái)沒(méi)啥問(wèn)題,strong版本的屬性跟隨原對(duì)象的變化而變化诱建,copy版本的屬性不變其障。但是,假設(shè)調(diào)用
[self.cpMutStr appendString:@"789"];
程序會(huì)崩潰涂佃。崩潰信息如下:
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'
*** First throw call stack:
原因很明顯励翼,是向NSTaggedPointerString對(duì)象發(fā)了一個(gè)它不能識(shí)別的selector。原因是copy版本的NSMutableString屬性本質(zhì)上不是一個(gè)NSMutableString對(duì)象辜荠,而是一個(gè)NSTaggedPointerString對(duì)象汽抚,它是一個(gè)不可變對(duì)象。該對(duì)象是NSMutableString對(duì)象執(zhí)行copy得來(lái)的伯病,還記得我們上一節(jié)的結(jié)論嗎造烁?對(duì)一個(gè)對(duì)象執(zhí)行copy得到的是一個(gè)不可變的對(duì)象。
針對(duì)NSArray午笛、NSDictionary惭蟋、NSSet等具有Mutable版本的類(lèi)進(jìn)行試驗(yàn)出現(xiàn)跟NSString類(lèi)似的現(xiàn)象。
3 結(jié)論
- 不可變類(lèi)型屬性药磺,推薦使用copy告组,因?yàn)榧僭O(shè)該對(duì)象實(shí)際上指向的是一個(gè)mutable的對(duì)象,mutable對(duì)象的改變不會(huì)導(dǎo)致該對(duì)象的改變癌佩;假設(shè)指向的不是mutable的對(duì)象木缝,那么copy和strong是等價(jià),因?yàn)閷?duì)一個(gè)不可變對(duì)象調(diào)用一次copy什么事情都不發(fā)生围辙,_cpID = [cpID copy] 等價(jià)于_cpID = cpID我碟。
- 可變類(lèi)型屬性,不能使用copy姚建,因?yàn)閏opy產(chǎn)生的對(duì)象是一個(gè)不可變對(duì)象矫俺,跟屬性描述是沖突的。