Storage of blocks in an array

In Objective-C, I know that blocks are considered objects, so I was wondering if they could be stored in an array. This begs the question, are blocks of objects of the first class or are they simply considered as objects for the sake of transferring them between objects? If they are objects of the first class, then should they not be stored in arrays?

+44
memory-management objective-c objective-c-blocks
Nov 03 '11 at 15:38
source share
2 answers

EDIT: Without going into details, under ARC you can now add blocks to collections, like any other object (see discussion).

I left the original answer intact, as it contains some interesting technical details.




This begs the question, are the blocks of objects of the first class or were they simply considered as objects for the sake of transferring them between objects? If they are objects of the first class, then they should not be possible to store in arrays?

Blocks are Objective-C objects that behave very much like any other NSObject, with a few key differences:

  • Blocks are always generated by the compiler. They are effectively "alloc / init" ed at run time when execution passes over block declarations.

  • Blocks are initially created on the stack. Block_copy () or the copy method should be used to move the block to the heap if the block must survive the current scope (see ARC clause below).

  • Blocks really don't have an API that can go beyond memory management.

  • To put a block in a collection, you must first copy it . <Blow> Always. Including in ARC. (See Comments.) If you do not, there is a risk that the stack designated by the Block will be autoreleased and your application will crash later.

  • Copying a block based on the stack will also copy all captured state. If you make multiple copies of a block, it is more efficient to copy it once, and then copy the copy (because copying the copy simply interferes with the save count, since the blocks are immutable).

  • The block from the method or function "just works" is returned to ARC; it will be automatically copied to the heap, and the return will be in fact an automated unit (the compiler can optimize auto-advertising under certain circumstances). Even with ARC, you still need to copy the block before pasting it into the collection.

I wrote a couple of blog posts that provide an introduction to the blocks and some tips and tricks . You may find them interesting.

And, yes, adding them to dictionaries is very useful. I wrote a couple of bits of code in which I deleted blocks in dictionaries as command handlers, where the key was the name of the command. Very comfortably.

+83
Nov 03 '11 at 17:16
source share

Yes, blocks are really objects, and you can put them in arrays:

 NSMutableArray *arr = [NSMutableArray new]; [arr addObject:^(){NSLog(@"my block");}]; void (^ myblock)() = [arr objectAtIndex:0]; myblock(); 

this will put "my block" in the console.

+35
Nov 03 2018-11-11T00:
source share



All Articles