Are Objective-C blocks autoreleased?

拜拜、爱过 提交于 2019-12-03 17:01:23

问题


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.



来源:https://stackoverflow.com/questions/4667278/are-objective-c-blocks-autoreleased

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!