如何在NSDictionary中调用块?

时间:2015-01-19 06:50:07

标签: objective-c nsdictionary block

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

当我在NSDictionary中调用块时,Xcode报告错误Called object type 'id' is not a function or function pointer

如何调用存储在NSDictionary中的块?

2 个答案:

答案 0 :(得分:2)

您应该创建一个局部变量来将id转换为块类型。

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

答案 1 :(得分:0)

只是演员问题,请尝试详细版本:

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