今天群里不知怎么說起了 block 在棧上還是在堆上的問題。好像之前在哪里看到過,現(xiàn)在 block 的屬性已經(jīng)不用寫 copy 關(guān)鍵字,就會自動 copy。于是做了幾個實(shí)驗(yàn),想看看什么情況下會自動 copy,什么情況下不會~
實(shí)驗(yàn)
代碼如下:
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;
}
實(shí)驗(yàn)結(jié)果:
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
- 函數(shù)傳參:
- 作為參數(shù)傳入函數(shù)不會被 copy
- 作為函數(shù)的返回值會被 copy
猜測
看著以上結(jié)論,感覺可以做出一個猜測:就是 block 被 retain 的時候就會自動被 copy,包括 autoRelease~ 這樣就能解釋為啥函數(shù)的參數(shù)不會被 copy,返回值就會被 copy。是不是很有道理呢 =w=