//聯(lián)系人:石虎QQ: 1224614774昵稱:嗡嘛呢叭咪哄
API Reference對(duì)__block變量修飾符有如下幾處解釋:
//A powerful featureofblocks is that they can modifyvariablesinthesame lexical scope.Yousignal thatablock can modifyavariableusingthe__block storage type modifier.//At function level are __block variables. These are mutablewithintheblock(andtheenclosing scope)andare preservedifanyreferencing blockiscopiedtotheheap.
大概意思?xì)w結(jié)出來就是兩點(diǎn):
1.__block對(duì)象在block中是可以被修改、重新賦值的。
2.__block對(duì)象在block中不會(huì)被block強(qiáng)引用一次房蝉,從而不會(huì)出現(xiàn)循環(huán)引用問題继低。
API Reference對(duì)__weak變量修飾符有如下幾處解釋:
__weak specifiesa referencethatdoesnotkeepthereferencedobjectalive.A weakreferenceissettonilwhenthere arenostrong referencestotheobject.
使用了__weak修飾符的對(duì)象日熬,作用等同于定義為weak的property耳胎。自然不會(huì)導(dǎo)致循環(huán)引用問題盒刚,因?yàn)樘O果文檔已經(jīng)說的很清楚顾犹,當(dāng)原對(duì)象沒有任何強(qiáng)引用的時(shí)候倒庵,弱引用指針也會(huì)被設(shè)置為nil。
因此炫刷,__block和__weak修飾符的區(qū)別其實(shí)是挺明顯的:
1.__block不管是ARC還是MRC模式下都可以使用擎宝,可以修飾對(duì)象,還可以修飾基本數(shù)據(jù)類型浑玛。
2.__weak只能在ARC模式下使用绍申,也只能修飾對(duì)象(NSString),不能修飾基本數(shù)據(jù)類型(int)顾彰。
3.__block對(duì)象可以在block中被重新賦值极阅,__weak不可以。
PS:__unsafe_unretained修飾符可以被視為iOS SDK 4.3以前版本的__weak的替代品涨享,不過不會(huì)被自動(dòng)置空為nil筋搏。所以盡可能不要使用這個(gè)修飾符
謝謝!!!