开发者

What is this objective-c function construct called?

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?


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.


Blocks and block variables.

Here's some reading:

http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Blocks/Articles/00_Introduction.html

http://pragmaticstudio.com/blog/2010/7/28/ios4-blocks-1

http://thirdcog.eu/pwcblocks/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜