關于 block 會不會被自動 copy 的實驗和猜想
今天群里不知怎么說起了 block 在棧上還是在堆上的問題。好像之前在哪里看到過,現在 block 的屬性已經不用寫 copy 關鍵字,就會自動 copy。于是做了幾個實驗,想看看什么情況下會自動 copy,什么情況下不會~
實驗
代碼如下:
TestClass.h
typedef void(^SimpleBlock)();
@interface TestClass : NSObject
@property (nonatomic, copy) SimpleBlockcopyProperty;
@property (nonatomic, strong) SimpleBlockstrongProperty;
@property (nonatomic, weak) SimpleBlockweakProperty;
@property (nonatomic, assign) SimpleBlockassignProperty;
@end
main
#import "TestClass.h"
SimpleBlocksomeFunction(SimpleBlockblock) {
NSLog(@"block as param : %@", block);
return block;
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
__blockint a = 1;
NSLog(@"orginal block : %@", ^{
a = 2;
});
// as a variable
SimpleBlockblock = ^{
a = 2;
};
NSLog(@"block as variable : %@", block);
__weakSimpleBlockweakBlock = ^{
a = 2;
};
NSLog(@"block as a weak variable : %@", weakBlock);
// as properties
TestClass* testClass = [TestClassnew];
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] orginalblock :
2017-02-06 17:43:36.207436 test2[27378:1079138] blockas variable :
2017-02-06 17:43:36.207457 test2[27378:1079138] blockas a weakvariable :
2017-02-06 17:43:36.207492 test2[27378:1079138] copyproperty :
2017-02-06 17:43:36.207517 test2[27378:1079138] strongproperty :
2017-02-06 17:43:36.207563 test2[27378:1079138] weakproperty :
2017-02-06 17:43:36.207581 test2[27378:1079138] assignproperty :
2017-02-06 17:43:36.207611 test2[27378:1079138] blockas param :
2017-02-06 17:43:36.207769 test2[27378:1079138] blockas return value :
分析
- 作為變量:
- 一個 block 剛聲明的時候是在棧上
- 賦值給一個普通變量之后就會被 copy 到堆上
- 賦值給一個 weak 變量不會被 copy
- 作為屬性:
- 用 strong 和 copy 修飾的屬性會被 copy
- 用 weak 和 assign 修飾的屬性不會被 copy
- 函數傳參:
- 作為參數傳入函數不會被 copy
- 作為函數的返回值會被 copy
猜測
看著以上結論,感覺可以做出一個猜測:就是 block 被 retain 的時候就會自動被 copy,包括 autoRelease~ 這樣就能解釋為啥函數的參數不會被 copy,返回值就會被 copy。是不是很有道理呢 =w=
來自:http://ios.jobbole.com/92865/
本文由用戶 許風穎 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!