Scrungepipes
Scrungepipes

Reputation: 37581

What retain policy should be used for a block as an object property?

I have the following initializer:

- (id) initWithBlock:(void(^)void) block;

and within the initializer I want to assign the block to a property so that it can be executed at a later time.

typedef void(^block)(void);
@interface myClass()
@property (X, nonatomic)    block theBlock;
@end

What should X be and why? (using ARC)

Upvotes: 0

Views: 108

Answers (2)

Jody Hagins
Jody Hagins

Reputation: 28349

You need to copy a block.

If you want details, please see an article by Mike Ash on the topic. Even if you don't want the details you should read it.

http://www.mikeash.com/pyblog/friday-qa-2009-08-14-practical-blocks.html

Upvotes: 1

Heiberg
Heiberg

Reputation: 5517

You should use copy. A block which has not been copied lives on the stack. Retaining something on the stack would be meaningless.

Upvotes: 0

Related Questions