views:

44

answers:

3

Hi All: I am a Iphone freshMan

today a wired parameter confused me,

- (UIBackgroundTaskIdentifier)beginBackgroundTaskWithExpirationHandler:(void(^)(void))handler

/* ... */

bgTask = [application beginBackgroundTaskWithExpirationHandler: ^{
    dispatch_async(dispatch_get_main_queue(), ^{
        [application endBackgroundTask:self->bgTask];
        self->bgTask = UIBackgroundTaskInvalid;
    });
}];

what is void(^)

I never met this before,i hope some one could help me

thanks!

+1  A: 

That's Objective-C "blocks" syntax. Have a look at http://developer.apple.com/library/ios/featuredarticles/Short_Practical_Guide_Blocks/index.html#//apple_ref/doc/uid/TP40009758

Graham Perks
Thank you my friend!
BPS1945
A: 

http://thirdcog.eu/pwcblocks/

Nimrod
A: 

(void(^)(void))handler means that the "handler" parameter is an Objective-C block that takes no arguments and returns nothing.

In your example, the handler block is:

 ^{
    dispatch_async(dispatch_get_main_queue(), ^{
        [application endBackgroundTask:self->bgTask];
        self->bgTask = UIBackgroundTaskInvalid;
    });
  }

The contents of the block are what's inside the "^{...}". Note that this is an example of a nested block: the block consists of a function call to dispatch_async, which in turn takes a block parameter.

David Gelhar
Thank you my friend!
BPS1945