__Block與__Weak區別,__Block__Weak區別

來源:互聯網
上載者:User

__Block與__Weak區別,__Block__Weak區別

一、Blocks理解:

Blocks可以訪問局部變數,但是不能修改,

聲明block的時候實際上是把當時的臨時變數又複製了一份,

在block裡即使修改了這些複製的變數,也不影響外面的原始變數。即所謂的閉包。

 

如果修改局部變數,需要加__block。

 

API Reference對__block變數修飾符有如下幾處解釋

//A powerful feature of blocks is that they can modify variables in the same lexical scope. You signal that a block can modify a variable using the __block storage type modifier. //At function level are __block variables. These are mutable within the block (and the enclosing scope) and are preserved if any referencing block is copied to the heap.

大概意思歸結出來就是兩點: 
1.__block對象在block中是可以被修改、重新賦值的。 
2.__block對象在block中不會被block強引用一次,從而不會出現循環參考問題。

 

二、__weak的理解

__weak __typeof(&*self)weakSelf =self; 等同於

__weak UIViewController *weakSelf =self;

為什麼不用__block 是因為通過引用來訪問self的執行個體變數 ,self被retain,block也是一個強引用,

引起循環參考,用__week是弱引用,當self釋放時,weakSelf已經等於nil。

 

API Reference對__weak變數修飾符有如下幾處解釋:

__weak specifies a reference that does not keep the referenced object alive. A weak reference is set to nil whenthere are no strong references to the object.

使用了__weak修飾符的對象,作用等同於定義為weak的property。自然不會導致循環參考問題,因為蘋果文檔已經說的很清楚,當原對象沒有任何強引用的時候,弱引用指標也會被設定為nil。

 

因此,__block和__weak修飾符的區別其實是挺明顯的: 
1.__block不管是ARC還是MRC模式下都可以使用,可以修飾對象,還可以修飾基礎資料型別 (Elementary Data Type)。 
2.__weak只能在ARC模式下使用,也只能修飾對象(NSString等),不能修飾基礎資料型別 (Elementary Data Type)(int)。 
3.__block對象可以在block中被重新賦值,__weak不可以。 
PS:__unsafe_unretained修飾符可以被視為iOS SDK 4.3以前版本的__weak的替代品,不過不會被自動置空為nil。所以儘可能不要使用這個修飾符。

相關文章

聯繫我們

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