關於 block 會不會被自動 copy 的實驗和猜想

戴倉薯發表於2019-03-04

今天群裡不知怎麼說起了 block 在棧上還是在堆上的問題。好像之前在哪裡看到過,現在 block 的屬性已經不用寫 copy 關鍵字,就會自動 copy。於是做了幾個實驗,想看看什麼情況下會自動 copy,什麼情況下不會~

實驗

程式碼如下:

TestClass.h
typedef void(^SimpleBlock)();

@interface TestClass : NSObject

@property (nonatomic, copy) SimpleBlock copyProperty;

@property (nonatomic, strong) SimpleBlock strongProperty;

@property (nonatomic, weak) SimpleBlock weakProperty;

@property (nonatomic, assign) SimpleBlock assignProperty;

@end複製程式碼
main
#import "TestClass.h"

SimpleBlock someFunction(SimpleBlock block) {
    NSLog(@"block as param : %@", block);
    return block;
}

int main(int argc, const char * argv[]) {
    @autoreleasepool {

        __block int a = 1;

        NSLog(@"orginal block : %@", ^{
            a = 2;
        });

        // as a variable
        SimpleBlock block = ^{
            a = 2;
        };
        NSLog(@"block as variable : %@", block);
        __weak SimpleBlock weakBlock = ^{
            a = 2;
        };
        NSLog(@"block as a weak variable : %@", weakBlock);

        // as properties
        TestClass* testClass = [TestClass new];
        testClass.weakProperty = ^{
            a = 2;
        };
        testClass.assignProperty = ^{
            a = 2;
        };
        testClass.copyProperty = ^{
            a = 2;
        };
        testClass.strongProperty = ^{
            a = 2;
        };

        NSLog(@"copy property : %@", testClass.copyProperty);
        NSLog(@"strong property : %@", testClass.strongProperty);
        NSLog(@"weak property : %@", testClass.weakProperty);
        NSLog(@"assign property : %@", testClass.assignProperty);

        NSLog(@"block as return value : %@", someFunction(^{
            a = 2;
        }));
    }
    return 0;
}複製程式碼

實驗結果:

2017-02-06 17:43:36.207212 test2[27378:1079138] orginal block : <__NSStackBlock__: 0x7fff5fbff728>
2017-02-06 17:43:36.207436 test2[27378:1079138] block as variable : <__NSMallocBlock__: 0x100402f70>
2017-02-06 17:43:36.207457 test2[27378:1079138] block as a weak variable : <__NSStackBlock__: 0x7fff5fbff6b8>
2017-02-06 17:43:36.207492 test2[27378:1079138] copy property : <__NSMallocBlock__: 0x100403140>
2017-02-06 17:43:36.207517 test2[27378:1079138] strong property : <__NSMallocBlock__: 0x100403170>
2017-02-06 17:43:36.207563 test2[27378:1079138] weak property : <__NSStackBlock__: 0x7fff5fbff668>
2017-02-06 17:43:36.207581 test2[27378:1079138] assign property : <__NSStackBlock__: 0x7fff5fbff640>
2017-02-06 17:43:36.207611 test2[27378:1079138] block as param : <__NSStackBlock__: 0x7fff5fbff618>
2017-02-06 17:43:36.207769 test2[27378:1079138] block as return value : <__NSMallocBlock__: 0x100600000>複製程式碼

分析

  • 作為變數:
    • 一個 block 剛宣告的時候是在棧上
    • 賦值給一個普通變數之後就會被 copy 到堆上
    • 賦值給一個 weak 變數不會被 copy
  • 作為屬性:
    • 用 strong 和 copy 修飾的屬性會被 copy
    • 用 weak 和 assign 修飾的屬性不會被 copy
  • 函式傳參:
    • 作為引數傳入函式不會被 copy
    • 作為函式的返回值會被 copy

猜測

看著以上結論,感覺可以做出一個猜測:就是 block 被 retain 的時候就會自動被 copy,包括 autoRelease~ 這樣就能解釋為啥函式的引數不會被 copy,返回值就會被 copy。是不是很有道理呢 =w=

感謝諸位大神對本次實驗的支援~

相關文章