iOS 深淺拷貝

來源:互聯網
上載者:User

標籤:

-(void)copyDemo{//    在非集合類對象中:對immutable對象進行copy操作,是指標複製,mutableCopy操作時內容複寫;對mutable對象進行copy和mutableCopy都是內容複寫。用代碼簡單表示如下://    //    [immutableObject copy] // 淺複製//    [immutableObject mutableCopy] //深複製//    [mutableObject copy] //深複製//    [mutableObject mutableCopy] //深複製    //    在集合類對象中,對immutable對象進行copy,是指標複製,mutableCopy是內容複寫;對mutable對象進行copy和mutableCopy都是內容複寫。但是:集合對象的內容複寫僅限於對象本身,對象元素仍然是指標複製。用代碼簡單表示如下://    //    [immutableObject copy] // 淺複製//    [immutableObject mutableCopy] //單層深複製//    [mutableObject copy] //單層深複製//    [mutableObject mutableCopy] //單層深複製    //    Baby *baby = [[Baby alloc] init]; //必須實現copy方法//    baby.babyName = @"wei";        NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithCapacity:10];    [dic setObject:@"1" forKey:@"key1"];    NSMutableArray *array1 = [[NSMutableArray alloc] initWithObjects:dic,dic, nil];    NSMutableArray *array2 = [array1 mutableCopy];//單層深拷貝    NSMutableArray *array3 = [[NSMutableArray alloc] initWithArray:array1 copyItems:YES];//裡面dic變成不可變了    NSMutableArray* array4 = [NSKeyedUnarchiver unarchiveObjectWithData:                                  [NSKeyedArchiver archivedDataWithRootObject: array1]];//裡面dic改變不影響其他數組(其與方法都會影響其他數組)    [array2 removeObjectAtIndex:0];    [[array4 objectAtIndex:0] setObject:@"3" forKey:@"key1"];//    [[array4 objectAtIndex:1] setBabyName:@"8888"];        DLog(@"%@ \n %@ \n %@ \n %@",array1,array2,array3,array4);}

 

iOS 深淺拷貝

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.