關於iOS的深copy和淺copy 以及copy 和 strong 的區別

來源:互聯網
上載者:User

標籤:字串   span   col   post   mutable   margin   end   str   好的   

之前都是靠記憶來維持copy和strong  也有問過我 。  我自己說起來才感覺都說不很清楚

通過簡單都代碼來驗證一下 。 有時候花點時間驗證一下才能讓思路更加清晰

    NSMutableString * mustr = [NSMutableString stringWithString:@"good"];

    

    self.str = mustr;

    

    self.fuzhiString = mustr;

    

    NSLog(@"self.str is (%@) ==== str,self.fuzhiString is (%@)",self.str,self.fuzhiString);

    

    NSLog(@"self.str *p is (%@) ==== str,self.fuzhiString *p is (%@)",self.str,self.fuzhiString);

 

    注釋:列印結果 == self.str is (good) ==== str,self.fuzhiString is (good)

    self.str *p is (0x600000059350) ==== str,self.fuzhiString *p is (0xa000000646f6f674)

    

    [mustr appendString:@"night"];

    

    NSLog(@"self.str is (%@) ==== str,self.fuzhiString is (%@)",self.str,self.fuzhiString);

    

    NSLog(@"self.str *p is (%@) ==== str,self.fuzhiString *p is (%@)",self.str,self.fuzhiString);

    

    注釋:列印結果 ==self.str is (goodnight) ==== str,self.fuzhiString is (good)

    self.str *p is (0x600000059350) ==== str,self.fuzhiString *p is (0xa000000646f6f674)

 

 

 

   列印結果: copy修飾的屬性為深copy(開闢一個新記憶體指向一個新的值)

                 strong修飾的屬性為淺copy(跟著mustr的值的改變而改變);

   同時表明字串為了防止不經意的修改 應該使用copy,養成好的習慣.

   當然用NSString用copy和strong是沒有什麼區別的

 

 

 

 

關於iOS的深copy和淺copy 以及copy 和 strong 的區別

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.