block語法裡直接訪問屬性會引發迴圈擁有

2021-07-01 23:38:06 字數 3449 閱讀 6657

注意不要用nsstring做實驗,nsstring的retaincount一直為-1;

1)區域性變數(無__block修飾):

只有當block被copy時,區域性變數的retaincount加1,同時self的retaincount也加1;

如果block沒有被copy那麼區域性變數的retaincount不變;block_release時不會把區域性和self的retaincount減1的(test1方法);

輸出:2012-11-26 12:02:55.822 blocktest[1843:11303] *****=無__block修飾的區域性變數

2012-11-26 12:02:55.823 blocktest[1843:11303] initial _lo1 retain count is 1

2012-11-26 12:02:55.824 blocktest[1843:11303] initial self retain count is 3

2012-11-26 12:02:55.824 blocktest[1843:11303] _lo1 retain count is 2

2012-11-26 12:02:55.825 blocktest[1843:11303] self retain count is 4

2012-11-26 12:02:55.825 blocktest[1843:11303] _lo1 retain count is 1

2012-11-26 12:02:55.826 blocktest[1843:11303] self retain count is 3

2)區域性變數(有__block修飾):區域性變數的引用數不會受block的影響, self的retaincount受block_copy影響加1(test2方法)

輸出:2012-11-26 12:02:55.826 blocktest[1843:11303] *****=有__block修飾的區域性變數

2012-11-26 12:02:55.826 blocktest[1843:11303] initial _blo1 retain count is 1

2012-11-26 12:02:55.827 blocktest[1843:11303] initial self retain count is 3

2012-11-26 12:02:55.827 blocktest[1843:11303] _blo1 retain count is 1

2012-11-26 12:02:55.828 blocktest[1843:11303] self retain count is 4

2012-11-26 12:02:55.828 blocktest[1843:11303] _blo1 retain count is 1

2012-11-26 12:02:55.828 blocktest[1843:11303] self retain count is 3

3)成員變數(無__block修飾):成員變數的retaincount不受影響,self的retaincount受block_copy影響加1(test3方法)

輸出:2012-11-26 12:02:55.829 blocktest[1843:11303] *****=無__block修飾的成員變數

2012-11-26 12:02:55.829 blocktest[1843:11303] initial _instancev retain count is 1

2012-11-26 12:02:55.830 blocktest[1843:11303] initial self retain count is 3

2012-11-26 12:02:55.830 blocktest[1843:11303] _instancev retain count is 1

2012-11-26 12:02:55.831 blocktest[1843:11303] self retain count is 4

2012-11-26 12:02:55.831 blocktest[1843:11303] _instancev retain count is 1

2012-11-26 12:02:55.831 blocktest[1843:11303] self retain count is 3

4)成員變數(有__block修飾):結論同3)(test4方法)

輸出:2012-11-26 12:02:55.832 blocktest[1843:11303] *****=有__block修飾的成員變數

2012-11-26 12:02:55.832 blocktest[1843:11303] initial _instancev2 retain count is 1

2012-11-26 12:02:55.833 blocktest[1843:11303] initial self retain count is 3

2012-11-26 12:02:55.833 blocktest[1843:11303] _instancev2 retain count is 1

2012-11-26 12:02:55.833 blocktest[1843:11303] self retain count is 4

2012-11-26 12:02:55.847 blocktest[1843:11303] _instancev2 retain count is 1

2012-11-26 12:02:55.847 blocktest[1843:11303] self retain count is 3

5)block中不引用任何外部變數:沒有block_copy這行**self的retaincount是不會加1的

輸出:2012-11-26 12:07:51.966 blocktest[1881:11303] *****=block中不引用任何外部變數

2012-11-26 12:07:51.967 blocktest[1881:11303] initial self retain count is 3

2012-11-26 12:07:51.967 blocktest[1881:11303] self retain count is 4

2012-11-26 12:07:51.967 blocktest[1881:11303] self retain count is 4

2012-11-26 12:07:51.968 blocktest[1881:11303] self retain count is 3

綜上:

1)無__block修飾的區域性變數只有在block_copy時retaincount才會加1;

2)有__block修飾的區域性變數和成員變數的retaincount不受block_copy的影響;

3)block_release不會把引用的外部變數和self的retaincount減1;

4)block_copy會把self的retaincount加1,無論block內部有沒有引用區域性和成員變數;

所以block_copy慎用!block_release時不要忘了把區域性變數和self也release一次;

Objective C 屬性訪問or直接訪問?

當定義了乙個屬性的時候,例如 property nonatomic,strong uiimageview imageview 有兩種訪問方法self.imageview imageview,屬性訪問和直接訪問有幾個區別 1直接訪問不用經過 方法派發 速度上有一定的優勢 2直接訪問,繞過了 記憶體管理...

從主頁直接訪問

最成功的乙個設計策略是在主頁上放置一些鏈結,這樣使用者能夠直接到達少數非常重要的功能。無論你將資訊結構組織得如何好,也不管你在導航系統中對它的表示是如何的透明,如果使用者必須經過多級的導航才能夠到達所需的功能,他們仍可能會迷失方向或者失去耐心。主頁上的直接鏈結為它提供了捷徑並簡化了這個任務。不幸的是...

Nginx直接訪問redis,lua訪問redis

直接去wget就可以 這個需要重新編譯nginx,然後把這個模組加上。下面大概說一下編譯nginx 和加這個模組可能會遇到的問題。首先如果你電腦上有nginx 那麼你要寫 使用nginx v 檢視一下當前的模組,把這些模組全部拷貝在乙個位置上。configure prefix 然後上面拷貝的全加上,...