Pablo
Pablo

Reputation: 29519

What type of objects a block will retain in the closure?

How can I say which type of objects a closure will retain? Is there any particular rule or list of types that closure will/will not retain?

Upvotes: 0

Views: 141

Answers (1)

zneak
zneak

Reputation: 138171

All Objective-C objects are retained. Everything else is simply copied. For example, here are three variables:

NSArray* array = [NSArray array];
int i = 0;
int* ptr = malloc(sizeof(int));

All three variables can be read from inside a block. However, only array will be retained, since it is the only variable on which it's possible to call the retain method.

Special care should be taken to make sure that the place ptr points to still exists when the block is executed, since it is a pointer that cannot be retained.

Upvotes: 1

Related Questions