iOS 深拷貝和淺拷貝

淺拷貝就是拷貝后,并沒有進行真正的復制,拷貝后的對象和原對象都指向同一塊內(nèi)存地址

深拷貝是真正的復制了一份,復制的對象指向了新的內(nèi)存地址

淺拷貝就相當于自己的影子,而深拷貝則是克隆的你

//注意
//copy 在拷貝不可變對象的時候為淺拷貝,但是拷貝可變對象的時候為深拷貝
//mutablecopy 始終為深拷貝

copy

NSLog(@"-----------------copy-----------------");
//不可變對象
NSString *str = @"字符串";
NSString *copyStr = str.copy;
NSLog(@"\n不可變字符串");
NSLog(@"\nstr_p:%p,\ncopyStr_p:%p",str,copyStr);
//    不可變字符串
//    str_p:0x1043b72a8,
//    copyStr_p:0x1043b72a8

NSArray *array = [NSArray array];
NSArray *copyArray = array.copy;
NSLog(@"\n不可變數(shù)組");
NSLog(@"\narray_p:%p,\ncopyArray_p:%p",array,copyArray);
//    不可變數(shù)組
//    array_p:0x6040000091f0,
//    copyArray_p:0x6040000091f0

//copy在拷貝不可變對象的時候為淺拷貝,淺拷貝的對象共用同一塊內(nèi)存地址



//可變對象
NSMutableString *mutablestr = [[NSMutableString alloc] initWithString:@"可變字符串"];
NSString *copyMutablestr = mutablestr.copy;
NSLog(@"\n可變字符串");
NSLog(@"\nmutablestr_p:%p,\ncopyMutablestr_p:%p",mutablestr,copyMutablestr);
//    可變字符串
//    mutablestr_p:0x60000005a700,
//    copyMutablestr_p:0x600000059c50

NSMutableArray *mutableArray = [NSMutableArray arrayWithCapacity:0];
NSArray *copyMutableArray = mutableArray.copy;//可變數(shù)組copy后,雖然為新的對象,但是對象類型是NSArray而不是NSMutableArray
NSLog(@"\n可變數(shù)組");
NSLog(@"\nmutableArray_p:%p,\ncopyMutableArray_p:%p",mutableArray,copyMutableArray);
//    可變數(shù)組
//    mutableArray_p:0x604000457a90,
//    copyMutableArray_p:0x6040000091f0

由此可得出結(jié)論
copy在拷貝可變對象的時候為深拷貝,深拷貝的對象為全新的對象,有自己的內(nèi)存地址
拷貝不可變對象的時候為淺拷貝,淺拷貝的對象共用同一塊內(nèi)存地址

mutablecopy

NSLog(@"-----------------mutableCopy-----------------");
NSString *str = @"字符串";
NSString *mutableCopyStr = str.mutableCopy;
NSLog(@"\n不可變字符串");
NSLog(@"\nstr_p:%p,\nmutableCopyStr:%p",str,mutableCopyStr);
//    不可變字符串
//    str_p:0x10aa092c8,
//    mutableCopyStr:0x600000041f20

NSArray *array = [NSArray array];
NSArray *mutablecopyArray = array.mutableCopy;
NSLog(@"\n不可變數(shù)組");
NSLog(@"\narray_p:%p,\nmutablecopyArray_p:%p",array,mutablecopyArray);
//    不可變數(shù)組
//    array_p:0x6000000034e0,
//    mutablecopyArray_p:0x604000256980

//可變對象
NSMutableString *mutablestr = [[NSMutableString alloc] initWithString:@"可變字符串"];
NSString *mutablecopyMutablestr = mutablestr.mutableCopy;
NSLog(@"\n可變字符串");
NSLog(@"\nmutablestr_p:%p,\nmutablecopyMutablestr_p:%p",mutablestr,mutablecopyMutablestr);
//    可變字符串
//    mutablestr_p:0x600000241f50,
//    mutablecopyMutablestr_p:0x60000005f440


NSMutableArray *mutableArray = [NSMutableArray arrayWithCapacity:0];
NSArray *mutablecopyMutableArray = mutableArray.mutableCopy;//
NSLog(@"\n可變數(shù)組");
NSLog(@"\nmutableArray_p:%p,\nmutablecopyMutableArray_p:%p",mutableArray,mutablecopyMutableArray);
//    可變數(shù)組
//    mutableArray_p:0x604000255060,
//    mutablecopyMutableArray_p:0x6040002555d0

由此可得出結(jié)論:mutablecopy都是深拷貝,拷貝后的對象都有自己的內(nèi)存地址

當容器內(nèi)有元素的時候,對對象的copy和mutablecopy會對里面的元素有啥影響呢?

NSMutableArray *mutableArray = [NSMutableArray arrayWithObjects:[NSMutableString stringWithString:@"1"],@"2", nil];
NSMutableArray *copyMutableArray = mutableArray.copy;
NSMutableArray *mutableCopyMutableArray = mutableArray.mutableCopy;

NSLog(@"mutableArray_p:%p,copyMutableArray_p:%p,mutableCopyMutableArray_p:%p",mutableArray,copyMutableArray,mutableCopyMutableArray);
//mutableArray_p:0x60000025c0e0,
//copyMutableArray_p:0x600000231980,
//mutableCopyMutableArray_p:0x60000025e120

//拷貝前
NSLog(@"mutableArray_object1_p:%p,mutableArray_object2_p:%p",mutableArray[0],mutableArray[1]);
//mutableArray_object1_p:0x60000025da90,
//mutableArray_object2_p:0x10d9b34c8

//copy后
NSLog(@"copyMutableArray_object1_p:%p,copyMutableArray_object2_p:%p",copyMutableArray[0],copyMutableArray[1]);
//copyMutableArray_object1_p:0x60000025da90,
//copyMutableArray_object2_p:0x10d9b34c8

//mutablecopy后
NSLog(@"mutableCopyMutableArray_object1_p:%p,mutableCopyMutableArray_object2_p:%p",mutableCopyMutableArray[0],mutableCopyMutableArray[1]);
//mutableCopyMutableArray_object1_p:0x60000025da90,
//mutableCopyMutableArray_object2_p:0x10d9b34c8

由此可見,copy和mutablecopy對于容器內(nèi)的元素是淺拷貝

如果你想對容器做完全拷貝(也就是容器深拷貝,元素也深拷貝)該怎么辦呢?可以使用歸檔的辦法(如果是自定義對象,需要實現(xiàn)NSCoding協(xié)議,不然會crash)。

NSArray *array = [NSArray arrayWithObjects:[NSMutableString stringWithString:@"1"],@"2", nil];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:array];
NSArray *mutablecopyArray = [NSKeyedUnarchiver unarchiveObjectWithData:data];

NSLog(@"array_p:%p,array_objec1_p:%p,array_objec2_p:%p",array,array[0],array[1]);
//array_p:0x6040002386c0,
//array_objec1_p:0x60400025f590,
//array_objec2_p:0x10d17b4c8
NSLog(@"mutablecopyArray_p:%p,mutablecopyArray_objec1_p:%p,mutablecopyArray_objec2_p:%p",mutablecopyArray,mutablecopyArray[0],mutablecopyArray[1]);
//mutablecopyArray_p:0x604000238780,
//mutablecopyArray_objec1_p:0x60400005ce60,
//mutablecopyArray_objec2_p:0xa000000000000321

自定義對象的歸檔協(xié)議實現(xiàn)

#import "Student.h"
@interface Student ()<NSCoding>
@property (nonatomic,copy)NSString *age;
@end
@implementation Student

/**********編碼/解碼***********/
//解碼的時候調(diào)用
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    self.age = [aDecoder decodeObjectForKey:@"age"];
    return self;
}
//編碼的時候調(diào)用
- (void)encodeWithCoder:(NSCoder *)aCoder
{
    [aCoder encodeObject:self.age forKey:@"age"];
}
@end

自定義對象的copy和mutablecopy,需要實現(xiàn)NSCopying、NSMutableCopying協(xié)議,不然使用copy和mutablecopy方法會crash

#import "Student.h"
@interface Student ()<NSCopying,NSMutableCopying>
@property (nonatomic,copy)NSString *age;
@end
@implementation Student

/******淺拷貝/深拷貝*******/
- (id)copyWithZone:(NSZone *)zone
{

    Student *s = [[[self class] allocWithZone:zone] init];
    s.age = self.age;
    return s;
}


- (id)mutableCopyWithZone:(NSZone *)zone
{

    Student *s = [[[self class] allocWithZone:zone] init];
    s.age = self.age;
    return s;
}
@end
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

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

  • 在工作中,有時會涉及到深拷貝和淺拷貝的內(nèi)容,發(fā)現(xiàn)有些地方理解的不夠透徹,所以在網(wǎng)上搜集資料總結(jié)一下,主要分四個方面...
    LeverTsui閱讀 3,650評論 3 5
  • 一、名詞解釋 1、 淺拷貝(shallow copy):只是增加了一個對被引用對象的一個指向,即指針拷貝,只是re...
    遠方的楓葉閱讀 436評論 0 1
  • 深拷貝:內(nèi)容拷貝,拷貝出來的對象和之前的對象的地址不一樣。 淺拷貝:指針拷貝,拷貝出來的對象和之前的對象的地址一樣...
    Persistence__閱讀 318評論 0 0
  • 1、對象拷貝有兩種方式:淺復制和深復制。顧名思義,淺復制,并不拷貝對象本身,僅僅是拷貝指向?qū)ο蟮闹羔?;深復制是直?..
    滴答大閱讀 867評論 0 2
  • 深拷貝和淺拷貝這個問題在面試中常常被問到,而在實際開發(fā)中,只要稍有不慎,就會在這里出現(xiàn)問題。尤其對于初學者來說,我...
    西門淋雨閱讀 1,927評論 0 1

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