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強引用一次,從而不會出現循環引用問題。code
API Reference對__weak變量修飾符有以下幾處解釋:對象
__weak specifies a reference that does not keep the referenced object alive. A weak reference is set to nil when there are no strong references to the object.
使用了__weak修飾符的對象,做用等同於定義爲weak的property。天然不會致使循環引用問題,由於蘋果文檔已經說的很清楚,當原對象沒有任何強引用的時候,弱引用指針也會被設置爲nil。ci
所以,__block和__weak修飾符的區別實際上是挺明顯的:
1.__block無論是ARC仍是MRC模式下均可以使用,能夠修飾對象,還能夠修飾基本數據類型。
2.__weak只能在ARC模式下使用,也只能修飾對象(NSString),不能修飾基本數據類型(int)。
3.__block對象能夠在block中被從新賦值,__weak不能夠。
PS:__unsafe_unretained修飾符能夠被視爲iOS SDK 4.3之前版本的__weak的替代品,不過不會被自動置空爲nil。因此儘量不要使用這個修飾符。文檔