If I declare a block like this ^{ DoSomething; }
and put it in an instance variable, do I need to Block_copy()
if I'm going to keep it around?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Yes, you need to copy. Not because they are autoreleased, but because they start on the stack. Note that blocks also behave like regular Objective-C objects, so that you can copy them using the regular copy
message:
void storeBlockForLater: (dispatch_block_t) block
{
[someArray addObject:[[block copy] autorelease]];
}
Or, if you have a block property:
@property(copy) dispatch_block_t block;
Retaining does not help here.