今天群里不知怎么说起了 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=