0

I was looking through one of Apple's XCode tutorials and came across something that looked like this inside the implementation of a class method:

void (^foo)(void);

foo = ^(void) {

    NSLog(@"Hello, World!");
};

foo();

Now obviously this is some kind of function declaration, implementation, and usage. However, I'd like to know more about it, what it is called, and what its limitations and advantages are. My searches online are turning up nothing relevant. Can anyone point me in the proper direction?

Tom
  • 18,685
  • 15
  • 71
  • 81
  • 1
    Apple added something called *blocks* to `Objective C` - these are somewhat like *closures* - see e.g. http://stackoverflow.com/questions/1462245/using-objective-c-blocks – Paul R Jun 20 '11 at 21:32

2 Answers2

4

They're called blocks. You can think of a block as a chunk of code that you can pass around to other parts of your program. They were added by Apple to its C and Objective-C compilers relatively recently, but some newer APIs take blocks instead or in addition to selectors or function pointers.

mipadi
  • 398,885
  • 90
  • 523
  • 479