Yong
Yong

Reputation: 758

How to invoke block in NSDictionary?

NSMutableDictionary* actions = [[NSMutableDictionary alloc] init];
actions[@"run"] = ^ () {
  NSLog(@"Hello");
};
actions[@"run"]();

Xcode reports the error Called object type 'id' is not a function or function pointer when I invoke the block in NSDictionary.

How I can invoke block stored in NSDictionary?

Upvotes: 2

Views: 764

Answers (2)

Andrea
Andrea

Reputation: 26383

Is just a problem of cast,try the verbose version :

(void) (^dictBlock)(void)) = actions[@"run"];
dictClock()

Upvotes: 0

Andrew Romanov
Andrew Romanov

Reputation: 5076

You should create a local variable to cast id to block type.

void(^block)(void) = [actions objectForKey:@"run"];
if(block)
{
   block();
}

Upvotes: 2

Related Questions