9

iPhone SDK 4.0 には、 type の 2 番目のパラメーターを必要とするUIApplication新しいメソッドがあります。setKeepAliveTimeout:void(^)(void)

-(BOOL)setKeepAliveTimeout:(NSTimeInterval)timeout handler:(void(^)(void))keepAliveHandler

2 番目のパラメーターの構文は正確には何を意味し、それに渡すことができる関数/ハンドラーをどのように宣言しますか?

FWIW以下は探しているものではありません...

void SomeHandler( void )
{
}
4

4 に答える 4

23

It is a "block", a new feature Apple added to C in Snow Leopard. Lots more info available at:

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

Block Objects

Block objects (informally, “blocks”) are an extension to C, as well as Objective-C and C++, that make it easy for programmers to define self-contained units of work. Blocks are similar to — but far more powerful than — traditional function pointers. The key differences are:

Blocks can be defined inline, as “anonymous functions.” Blocks capture read-only copies of local variables, similar to “closures” in other languages This is kind of functionality is common in dynamically-typed interpreted languages, but has never before been widely available to C programmers. Apple has published both the Blocks Languages Specification and our implementation as open source under the MIT license, added blocks support to GCC 4.2 and clang, and has submitted it for consideration as part of the next version of the C programming language.

Syntax

A block variable looks like a function pointer, except with a caret (‘^’) instead of an asterisk (‘*’).

void (^my_block)(void);
于 2010-08-17T03:50:52.070 に答える
8

そして、その特定の関数のコードは次のようになります。

[[UIApplication sharedApplication] setKeepAliveTimeout:5.0 handler:^{
    NSLog( @"This is my timeout handler" );
}];
于 2010-08-17T05:21:52.463 に答える
1

It means it take a block (of code, aka closure) see http://developer.apple.com/mac/articles/cocoa/introblocksgcd.html These are new to objective-c for OSX 10.6 and iOS 4

于 2010-08-17T03:51:44.937 に答える
0

関数は次のようにマスクできます。

#if NS_BLOCKS_AVAILABLE
- (void)foo;
#endif
于 2010-10-05T16:04:23.407 に答える