开发者

Are Objective-C blocks autoreleased?

开发者 https://www.devze.com 2023-02-04 07:15 出处:网络
If I declare a block like this ^{ DoSomething; } and put it in an insta开发者_开发技巧nce variable, do I need to Block_copy() if I\'m going to keep it around?Yes, you need to copy. Not because they ar

If I declare a block like this ^{ DoSomething; } and put it in an insta开发者_开发技巧nce variable, do I need to Block_copy() if I'm going to keep it around?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消