Objective-C中的深拷貝和淺拷貝

        在Objective-C中對象之間的拷貝分爲淺拷貝和深拷貝。說白了,對非容器類的淺拷貝就是拷貝對象的地址,對象裏面存的內容仍然是一份,沒有新的內存被分配。對非容器類的深拷貝就是重寫分配一塊內存,而後把另外一個對象的內容原封不動的給我拿過來。對容器類的深拷貝是對容器中的每一個元素都進行拷貝,容器類的淺拷貝是對容器裏的內容不進行拷貝,兩個容器的地址是不一樣的,但容器裏的所裝的東西是同樣的,在一個容器中修改值,則另外一個淺拷貝的容器中的值也會變化。因此對非容器類看對象是否爲深拷貝仍是淺拷貝就得看對象的內存地址就能夠看出來,而對容器類,咱們則進一步看容器中的內容了。由於OC中用引用計數的方式來進行內存管理的因此咱們也能夠經過觀察對象retainCount的變化來分析對象之間是不是深拷貝仍是淺拷貝。下面會經過對不一樣類型的對象進行測試來詳細的理解一下對象的深拷貝和淺拷貝。數組

        那麼對象大致都分爲哪些類型呢?從可變不可變和容器類非容器類的角度能夠把對象分爲一下幾種,那麼什麼是容器類呢?容器類就是用該類聲明的對象能夠去容納其餘對象,非容器類則沒有這些功能。那麼什麼是可變或者不可變的呢?可變的時內存的大小是能夠根據須要改變,而不可變的就是分配完之後就不能夠改變他的內存空間(以上是本人的理解,不足或理解偏頗之處還請批評指正,轉載本文請註明出處)app

                1. 非容器不可變對象,好比NSString測試

                2.非容器可變對象:好比NSMutableStringspa

                3.容器類不可變對象: 好比NSArraycode

                4.容器類可變對象: 好比NSMutableArrayorm

        在觀察深淺拷貝以前先得了解一下retain,copy和mutableCopy的特色,特色以下:對象

                1.retain:始終是淺複製。引用計數每次加一。返回對象是否可變與被複制的對象保持一致。blog

                2.copy:對於可變對象爲深複製,引用計數不改變;對於不可變對象是淺複製, 引用計數每次加一。始終返回一個不可變對象。  內存

                3.mutableCopy:始終是深複製,引用計數不改變。始終返回一個可變對象。ci

    下面把容器非容器,可變非可變結合着retain,copy,mutableCopy來仔細的分析一下OC中得深淺拷貝,代碼走起:

            1.非容器 + 不可變對象 + retain + copy + mutableCopy

                代碼說明:先定義一個非容器類不可變對象,而後同過retain,copy和mutableCopy的方式把值copy給一個非容器類不可變對象,最後把各個對象的地址輸出,用NSString來作測試。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
NSLog(@ "非容器類不可變對象拷貝NSString" );
NSString *str = @"ludashi";
NSLog(@ " str = %@,  init_str.retainCount   = %d" , str, ( int )str.retainCount);
 
//把str經過retain方式把值賦給str1
NSString *str1 = [str retain];
NSLog(@ "str1 = %@, retain_str1.retainCount = %d" ,str1, ( int )str1.retainCount);
 
//把str經過copy的方式把值賦給str2
NSString *str2 = [str copy];
NSLog(@ "str2 = %@, copy_str2.retainCount   = %d" , str2, ( int )str2.retainCount);
 
//把str經過mutableCopy的方式把值賦給str3
NSString *str3 = [str mutableCopy];
NSLog(@ "str3 = %@, mutableCopy_str3.retainCount = %d" , str3, ( int )str3.retainCount);
 
//分別輸出每一個字符串的內存地址
NSLog(@ " str-p = %p" , str);
NSLog(@ "str1-p = %p" , str1);
NSLog(@ "str2-p = %p" , str2);
NSLog(@ "str3-p = %p" , str3);

            代碼運行結果:

1
2
3
4
5
6
7
8
9
2014-08-06 10:49:04.422 Memory[761:303] 非容器類不可變對象拷貝NSString
2014-08-06 10:49:04.423 Memory[761:303]  str = ludashi,  init_str.retainCount   = -1
2014-08-06 10:49:04.424 Memory[761:303] str1 = ludashi, retain_str1.retainCount = -1
2014-08-06 10:49:04.424 Memory[761:303] str2 = ludashi, copy_str2.retainCount   = -1
2014-08-06 10:49:04.424 Memory[761:303] str3 = ludashi, mutableCopy_str3.retainCount = 1
2014-08-06 10:49:04.425 Memory[761:303]  str-p = 0x100004170
2014-08-06 10:49:04.425 Memory[761:303] str1-p = 0x100004170
2014-08-06 10:49:04.425 Memory[761:303] str2-p = 0x100004170
2014-08-06 10:49:04.426 Memory[761:303] str3-p = 0x100107790

              代碼運行結果分析:

                1. 對於非容器類的不可變對象retain和copy爲淺拷貝,mutableCopy爲深拷貝

                2. 淺拷貝得到的對象的地址和原有對象的地址一致

                3.而深拷貝返回新的內存地址,而且返回的對象爲可變對象 

    2.非容器 + 可變對象 + retain + copy + mutableCopy

        接下來咱們來測試下非容器類的可變對象的深淺拷貝

            代碼以下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
NSLog(@ "非容器類的可變對象拷貝" );
NSMutableString *s = [NSMutableString stringWithFormat:@ "ludashi" ];
NSLog(@ " s = %@,     init_s_retainCount = %d" , s, ( int )s.retainCount);
 
//把s經過retain的方式把值 賦給s1;
NSMutableString *s1 = [s retain];
NSLog(@ "s1 = %@,  retain_s1_retainCount = %d" , s1, ( int )s1.retainCount);
 
//把s經過copy的方式把值賦給s2;
NSMutableString *s2 = [s copy];
NSLog(@ "s2 = %@,    copy_s2_retianCount = %d" , s2, ( int )s2.retainCount);
 
//把s經過mutableCopy的方式把值賦給s3
NSMutableString *s3 = [s mutableCopy];
NSLog(@ "s3 = %@, mutable_s3_retainCount = %d" , s3, ( int )s3.retainCount);
 
 
//打印每一個非容器類可變對象的地址
NSLog(@ " s_p = %p" , s);
NSLog(@ "s1_p = %p" , s1);
NSLog(@ "s2_p = %p" , s2);
NSLog(@ "s3_p = %p" , s3);

           運行結果以下:

1
2
3
4
5
6
7
8
9
2014-08-06 10:49:04.429 Memory[761:303] 非容器類的可變對象拷貝
2014-08-06 10:49:04.430 Memory[761:303]  s = ludashi,     init_s_retainCount = 1
2014-08-06 10:49:04.430 Memory[761:303] s1 = ludashi,  retain_s1_retainCount = 2
2014-08-06 10:49:04.430 Memory[761:303] s2 = ludashi,    copy_s2_retianCount = 1
2014-08-06 10:49:04.431 Memory[761:303] s3 = ludashi, mutable_s3_retainCount = 1
2014-08-06 10:49:04.431 Memory[761:303]  s_p = 0x100107860
2014-08-06 10:49:04.431 Memory[761:303] s1_p = 0x100107860
2014-08-06 10:49:04.432 Memory[761:303] s2_p = 0x100206470
2014-08-06 10:49:04.432 Memory[761:303] s3_p = 0x1003004a0

             運行結果分析:

                1.retian對對可變對象爲淺拷貝

                2.copy對可變對象非容器類爲深拷貝

                3.mutableCopy對可變非容器類爲深拷貝

     3.容器類 +  非可變對象 + retain + copy + mutableCopy

        下面對容器類的非可變對象進行測試,有程序的運行結果可知當使用mutableCopy時確實返回了一個新的容器(由內存地址能夠看出),但從容器對象看而言是容器的深拷貝,但從輸出容器中的元素是容器的淺拷貝。那麼咱們如何實現容器的徹底拷貝呢?下面會介紹到。

    ​    ​    ​代碼以下:

    NSMutableString *string = [NSMutableString stringWithFormat:@"ludashi"];
    //第二種:容器類不可變對象拷貝
    NSLog(@"容器類不可變對象拷貝");
    NSArray *array = [NSArray arrayWithObjects:string, @"b", nil];
    NSLog(@" array[0] = %@,    init_array.retainCount = %d", array[0], (int)array.retainCount);
    
    //把array經過retain方式把值賦給array1
    NSArray *array1 = [array retain];
    NSLog(@"array1[0] = %@, retain_array1.retainCount = %d", array1[0], (int)array1.retainCount);
    
    //把array經過copy的方式把值賦給array2
    NSArray *array2 = [array copy];
    NSLog(@"array2[0] = %@,    copy_array.retainCount = %d", array2[0], (int)array2.retainCount);
    
    //把array經過mutableCopy方式把值賦給array3
    NSArray *array3 = [array mutableCopy];
    NSLog(@"array3[0] = %@, mutableCopy_array3.retainCount = %d", array3[0], (int)array3.retainCount);
    
    //分別輸出每一個地址
    NSLog(@"分別輸出每一個地址");
    NSLog(@" array_p = %p", array);
    NSLog(@"array1_p = %p", array1);
    NSLog(@"array2_p = %p", array2);
    NSLog(@"array3_p = %p", array3);
    
    //分別輸出每一個地址
    NSLog(@"分別輸出拷貝後數組中第一個元素的地址");
    NSLog(@" array_p[0] = %p", array[0]);
    NSLog(@"array1_p[0] = %p", array1[0]);
    NSLog(@"array2_p[0] = %p", array2[0]);
    NSLog(@"array3_p[0] = %p", array3[0]);

    ​    ​    ​運行結果:

2014-08-13 15:24:57.386 Memory[3678:303] 容器類不可變對象拷貝
2014-08-13 15:24:57.386 Memory[3678:303]  array[0] = ludashi,    init_array.retainCount = 1
2014-08-13 15:24:57.387 Memory[3678:303] array1[0] = ludashi, retain_array1.retainCount = 2
2014-08-13 15:24:57.387 Memory[3678:303] array2[0] = ludashi,    copy_array.retainCount = 3
2014-08-13 15:24:57.387 Memory[3678:303] array3[0] = ludashi, mutableCopy_array3.retainCount = 1
2014-08-13 15:24:57.388 Memory[3678:303] 分別輸出每一個地址
2014-08-13 15:24:57.388 Memory[3678:303]  array_p = 0x1005001b0
2014-08-13 15:24:57.388 Memory[3678:303] array1_p = 0x1005001b0
2014-08-13 15:24:57.389 Memory[3678:303] array2_p = 0x1005001b0
2014-08-13 15:24:57.389 Memory[3678:303] array3_p = 0x100107750
2014-08-13 15:24:57.389 Memory[3678:303] 分別輸出拷貝後數組中第一個元素的地址
2014-08-13 15:24:57.390 Memory[3678:303]  array_p[0] = 0x100500040
2014-08-13 15:24:57.390 Memory[3678:303] array1_p[0] = 0x100500040
2014-08-13 15:24:57.390 Memory[3678:303] array2_p[0] = 0x100500040
2014-08-13 15:24:57.390 Memory[3678:303] array3_p[0] = 0x100500040

 

    4.容器類 +  可變對象 + retain + copy + mutableCopy

        下面對容器類的可變對象進行測試,copy和mutableCopy對於容器自己是深拷貝,緣由是返回了一個新的容器地址,但對於容器中的元素仍然是淺拷貝。

            代碼以下:

    NSLog(@"********************************************\n\n\n\n");
    //第四種:容器類的可變對象的拷貝,用NSMutableArray來實現
    NSLog(@"容器類的可變對象的拷貝");
    
    
    
    NSMutableArray *m_array = [NSMutableArray arrayWithObjects:string, nil];
    NSLog(@" m_array[0] = %@,     init_m_array_retainCount = %d", m_array[0], (int)m_array.retainCount);
    
    //把m_array經過retain把值賦給m_array1
    NSMutableArray *m_array1 = [m_array retain];
    NSLog(@"m_array1[0] = %@,  retain_m_array1_retainCount = %d", m_array1[0], (int)m_array1.retainCount);
    
    //把m_array經過copy把值賦給m_array2
    NSMutableArray *m_array2 = [m_array copy];
    NSLog(@"m_array2[0] = %@,    copy_m_array2_retainCount = %d", m_array2[0], (int)m_array2.retainCount);
    
    //把m_array經過mytableCopy把值賦給m_array3
    NSMutableArray *m_array3 = [m_array mutableCopy];
    NSLog(@"m_array3[0] = %@, mutable_m_array3_retainCount = %d", m_array3[0], (int)m_array3.retainCount );
    
    //打印輸出每一個可變容器對象的地址
    NSLog(@"打印輸出每一個可變容器對象的地址");
    NSLog(@" m_array_p = %p", m_array);
    NSLog(@"m_array_p1 = %p", m_array1);
    NSLog(@"m_array_p2 = %p", m_array2);
    NSLog(@"m_array_p3 = %p", m_array3);
    
    //打印輸出每一個可變容器中元素的地址
    NSLog(@"打印輸出每一個可變容器中元素的地址");
    NSLog(@" m_array_p[0] = %p", m_array[0]);
    NSLog(@"m_array_p1[0] = %p", m_array1[0]);
    NSLog(@"m_array_p2[0] = %p", m_array2[0]);
    NSLog(@"m_array_p3[0] = %p", m_array3[0]);

    ​    ​運行結果:

2014-08-13 15:24:57.395 Memory[3678:303] 容器類的可變對象的拷貝
2014-08-13 15:24:57.395 Memory[3678:303]  m_array[0] = ludashi,     init_m_array_retainCount = 1
2014-08-13 15:24:57.395 Memory[3678:303] m_array1[0] = ludashi,  retain_m_array1_retainCount = 2
2014-08-13 15:24:57.396 Memory[3678:303] m_array2[0] = ludashi,    copy_m_array2_retainCount = 1
2014-08-13 15:24:57.396 Memory[3678:303] m_array3[0] = ludashi, mutable_m_array3_retainCount = 1
2014-08-13 15:24:57.396 Memory[3678:303] 打印輸出每一個可變容器對象的地址
2014-08-13 15:24:57.397 Memory[3678:303]  m_array_p = 0x1005002d0
2014-08-13 15:24:57.397 Memory[3678:303] m_array_p1 = 0x1005002d0
2014-08-13 15:24:57.397 Memory[3678:303] m_array_p2 = 0x100300670
2014-08-13 15:24:57.398 Memory[3678:303] m_array_p3 = 0x100205930
2014-08-13 15:24:57.398 Memory[3678:303] 打印輸出每一個可變容器中元素的地址
2014-08-13 15:24:57.398 Memory[3678:303]  m_array_p[0] = 0x100500040
2014-08-13 15:24:57.399 Memory[3678:303] m_array_p1[0] = 0x100500040
2014-08-13 15:24:57.399 Memory[3678:303] m_array_p2[0] = 0x100500040
2014-08-13 15:24:57.399 Memory[3678:303] m_array_p3[0] = 0x100500040

    ​    ​上面的代碼以及代碼的運行結果翻來覆去就是在驗證下面的結論:

​                1.retain:始終是淺複製。引用計數每次加一。返回對象是否可變與被複制的對象保持一致。

                2.copy:對於可變對象爲深複製,引用計數不改變;對於不可變對象是淺複製, 引用計數每次加一。始終返回一個不可變對象。  

                3.mutableCopy:始終是深複製,引用計數不改變。始終返回一個可變對象。

 

  自定義類對象之間的深淺拷貝問題

    在Objective-C中並非全部的類都支持拷貝;只有遵循NSCopying協議的類,才支持copy拷貝,只有遵循NSMutableCopying協議的類,才支持mutableCopy拷貝。若是沒有遵循拷貝協議,拷貝時會出錯。

     若是咱們想再咱們自定義的類中支持copy和mutableCopy那麼咱們就須要使咱們定義的類遵循NSCopying和NSMutableCopying協議,代碼以下:

1 @interface Test : NSObject <NSCopying, NSMutableCopying>

      而後再重寫-(id) copyWithZone : (NSZone *) zone  和 -(id)mutableCopyWithZone : (NSZone *) zone

      重寫-(id) copyWithZone :(NSZone *)zone方法以下

//淺拷貝
-(id) copyWithZone : (NSZone *) zone
{
    return [self retain];      
}


//深拷貝
-(id) mutableCopyWithZone : (NSZone *) zone
{
    Test *test = [[Test allocWithZone : zone] init];
    test.property = self.property;
    return test;          
}

 

  咱們如何實現容器中的徹底拷貝呢?上代碼最爲直接,上面真的沒有考慮到容器中元素拷貝的問題,下面的代碼補充一下上面的不足之處,下面的代碼給本身作一個進階吧!下面全是乾貨。 

    //新建一個測試字符串
    NSMutableString * str = [NSMutableString stringWithFormat:@"ludashi__"];
    
    //新建一個測試字典
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:1];
    [dic setObject:str forKey:@"key1"];
    
    //把字典存入數組中
    NSMutableArray *oldArray = [NSMutableArray arrayWithObject:dic];
    
    //用就得數組生成新的數組
    NSMutableArray *newArray = [NSMutableArray arrayWithArray:oldArray];
    
    //用copyItems拷貝數組中的元素
    NSMutableArray *copyItems = [[NSMutableArray alloc] initWithArray:oldArray copyItems:YES];
    
    //把數組歸檔成一個NSData,而後再實現徹底拷貝
    NSData * data = [NSArchiver archivedDataWithRootObject:oldArray];
    NSMutableArray *multable = [NSUnarchiver unarchiveObjectWithData:data];
    
    //往字典中加入新的值
    [dic setObject:@"new_value1" forKey:@"key2"];
    //改變str的值
    [str appendString:@"update"];
    NSLog(@"%@", oldArray);
    NSLog(@"%@", newArray);
    NSLog(@"%@", copyItems);
    NSLog(@"%@", multable);
    
    //每一個數組的地址爲:
    NSLog(@"%p", oldArray);
    NSLog(@"%p", newArray);
    NSLog(@"%p", copyItems);
    NSLog(@"%p", multable);

  上面的代碼運行結果:

2014-08-13 16:33:00.752 OC6-1[3942:303] (
        {
        key1 = "ludashi__update";
        key2 = "new_value1";
    }
)
2014-08-13 16:33:00.753 OC6-1[3942:303] (
        {
        key1 = "ludashi__update";
        key2 = "new_value1";
    }
)
2014-08-13 16:33:00.753 OC6-1[3942:303] (
        {
        key1 = "ludashi__update";
    }
)
2014-08-13 16:33:00.753 OC6-1[3942:303] (
        {
        key1 = "ludashi__";
    }
)
2014-08-13 16:33:00.754 OC6-1[3942:303] 0x100204560
2014-08-13 16:33:00.754 OC6-1[3942:303] 0x1002046d0
2014-08-13 16:33:00.754 OC6-1[3942:303] 0x1002047c0
2014-08-13 16:33:00.755 OC6-1[3942:303] 0x100406610
相關文章
相關標籤/搜索