深拷貝和淺拷貝之非容器類對(duì)象

1.對(duì)可變對(duì)象進(jìn)行copy操作

NSMutableString *mutaleStr = [NSMutableString stringWithString:@"mutaleStr"];
NSString *copyStr = [mutaleStr copy];
NSLog(@"mutaleStr:%p \n copyStr:%p",mutaleStr, copyStr);

結(jié)果:

mutaleStr:0x600000065400 
copyStr  :0x640420201410595

結(jié)論:對(duì)可變對(duì)象進(jìn)行copy操作是內(nèi)容copy(深copy)
猜想:如果將
NSString *copyStr = [mutaleStr copy];改為
NSMutableString *copyStr = [mutaleStr copy];后,進(jìn)行
[copyStr appendString:@"sss"];
會(huì)發(fā)生什么?
結(jié)論:copyStr在編譯時(shí)為NSMutableString,但是在運(yùn)行時(shí)為NSString類型,所以對(duì)copyStr進(jìn)行append等修改時(shí),編譯可通過(guò),但運(yùn)行時(shí)會(huì)崩潰。

2.對(duì)可變對(duì)象進(jìn)行mutableCopy操作

NSMutableString *mutaleStr = [NSMutableString stringWithString:@"mutaleStr"];
NSMutableString *copyStr = [mutaleStr mutableCopy];
NSLog(@"mutaleStr:%p \ncopyStr:%p",mutaleStr, copyStr);

結(jié)果:

mutaleStr:0x600000063180 
copyStr  :0x6000000631c0

結(jié)論:對(duì)可變對(duì)象進(jìn)行mutableCopy操作是是內(nèi)容copy(深copy)
猜想:如果將
NSMutableString *copyStr = [mutaleStr mutableCopy];改為
NSString *copyStr = [mutaleStr mutableCopy];后,進(jìn)行
[copyStr appendString:@"sss"];
會(huì)發(fā)生什么?
結(jié)論:copyStr在編譯時(shí)為NSString,在運(yùn)行時(shí)為NSMutableString,所以對(duì)copyStr進(jìn)行append等修改時(shí),編譯時(shí)就會(huì)報(bào)錯(cuò)。

3.對(duì)不可變對(duì)象進(jìn)行copy操作

NSString *str = [NSString stringWithFormat:@"mutaleStr"];
NSString *copyStr = [str copy];
NSLog(@"str:%p \ncopyStr:%p",str, copyStr);

結(jié)果

str    :0x640420201410595 
copyStr:0x640420201410595

結(jié)論:對(duì)不可變對(duì)象進(jìn)行copy操作是指針拷貝(淺拷貝)

4.對(duì)不可變對(duì)象進(jìn)行mutableCopy操作

NSString *str = [NSString stringWithFormat:@"mutaleStr"];
NSMutableString *mutableCopyStr = [str mutableCopy];
NSLog(@"str:%p \nmutableCopyStr:%p",str, mutableCopyStr);

結(jié)果:

str           :0x640420201410595 
mutableCopyStr:0x600000063180

結(jié)論:對(duì)不可變對(duì)象 進(jìn)行mutableCopy操作是內(nèi)容拷貝(深拷貝)
綜上所述:只有對(duì)不可變對(duì)象進(jìn)行copy操作是指針拷貝(淺拷貝),其他的都是內(nèi)容拷貝(深拷貝)

看文字看的想睡覺(jué),放張我喜歡的智妍刺激刺激昏昏欲睡的大腦

美不美

下面放幾個(gè)實(shí)例:

@property (nonatomic, copy) NSString *name;

NSMutableString類型的變量對(duì)name賦值:

NSMutableString *mutaleStr = [NSMutableString stringWithString:@"mutaleStr"];
//賦值
self.name = mutaleStr;
NSLog(@"\nmutaleStr:%p\nname:%p",mutaleStr,self.name);
NSLog(@"\nmutaleStr:%@\nname:%@",mutaleStr,self.name);
// 操作1
[mutaleStr appendString:@"appendString"];
// 操作2
// mutaleStr = [NSMutableString stringWithString:@"resetValue"];
NSLog(@"\n改變后........");
NSLog(@"\nmutaleStr:%p\nname:%p",mutaleStr,self.name);
NSLog(@"\nmutaleStr:%@\nname:%@",mutaleStr,self.name);
/*
當(dāng)name聲明為copy時(shí),為(內(nèi)容copy)深copy
操作1和操作2都不會(huì)對(duì)self.name產(chǎn)生影響,因?yàn)槭巧頲opy

當(dāng)name聲明為strong,為(指針copy)淺copy
賦值:指向同一個(gè)地址,如果想產(chǎn)生copy的效果,可以這么賦值self.name = [mutaleStr copy];
操作1:對(duì)mutaleStr進(jìn)行appendString,地址沒(méi)變,內(nèi)容改變了
結(jié)果:slef.name會(huì)隨著mutaleStr改變而改變
操作2:對(duì)mutaleStr重新賦值,mutaleStr的地址和內(nèi)容都改變了
結(jié)果:self.name不會(huì)變
 */

對(duì)NSString類型的變量對(duì)name進(jìn)行賦值:

NSString *str = [NSString stringWithFormat:@"NSString"];
self.name = str;
NSLog(@"\nstr:%p\nname:%p",str,self.name);
NSLog(@"\nstr:%@\nname:%@",str,self.name);
/*
 不論name聲明為strong還是copy都是淺拷貝
 */

結(jié)論:這就是為什么很多大牛推薦用copy的原因,因?yàn)槟悴恢涝诼暶鱏trong的情況下,會(huì)出現(xiàn)了莫名其妙的bug,所以說(shuō)論寫(xiě)代碼習(xí)慣的重要性。

@property (nonatomic, copy) NSMutableString *name;

NSMutableString類型的變量對(duì)name賦值:
結(jié)論同上,如果name聲明為strong的時(shí)候,如果對(duì)self.name進(jìn)行append操作,也會(huì)反向同樣影響mutaleStr的值

NSString類型的變量對(duì)name進(jìn)行賦值:
如果name是strong類型,則是深copy;
如果name是copy類型,則是淺copy,但是之前說(shuō)了,如果copy過(guò)來(lái)了,在編譯時(shí)是沒(méi)有問(wèn)題的,但是會(huì)在運(yùn)行時(shí)報(bào)錯(cuò).

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容