Nick Moore Nick Moore - 1 year ago 105
Objective-C Question

Storing a block in a collection

Is it possible to directly store a block in a collection such as NSArray?

Answer Source

Yes, blocks are valid Objective-C objects, following all the conventions of NSObject. Just do the copy/autorelease dance:

[array addObject:[[block copy] autorelease]];

For ARC - omit the copy and autorelease calls:

[array addObject:block];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download