【Objective_C學習筆記】alloc-init 與new的區別

來源:互聯網
上載者:User

標籤:

  在一些源碼中看到有人用[classname new]建立和初始化對象,但是我們大多數都用[[classname alloc] init],所以就很好奇兩者之間的區別,通過查看一些資料

  • http://stackoverflow.com/questions/11256228/what-is-the-difference-between-class-new-and-class-alloc-init-in-ios
  • http://www.cnblogs.com/ulihj/archive/2011/01/15/1936342.html

得出的結論是:

  1. [classname new]  = [[classname alloc] init];
  2. new使用的是預設的init方法,不太靈活,若使用alloc-init格式可調用自訂的初始化方法,帶參或者不帶參,靈活性較大;
  3. alloc-init顯示調用初始化方法,而new隱示調用初始化方法;

綜上所述,我覺得,若用預設的init方法,完全可以用[classname new] 建立和初始化對象簡化代碼;若要調用自訂的初始化方法,那還是用alloc-init吧,當然這還是隨大家自己的使用習慣。

【Objective_C學習筆記】alloc-init 與new的區別

聯繫我們

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