今天羣裏不知怎麼提及了 block 在棧上仍是在堆上的問題。好像以前在哪裏看到過,如今 block 的屬性已經不用寫 copy 關鍵字,就會自動 copy。因而作了幾個實驗,想看看什麼狀況下會自動 copy,什麼狀況下不會~javascript
代碼以下:java
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複製代碼
#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 被 retain 的時候就會自動被 copy,包括 autoRelease~ 這樣就能解釋爲啥函數的參數不會被 copy,返回值就會被 copy。是否是頗有道理呢 =w=atom
感謝諸位大神對本次實驗的支持~spa