【问题标题】:Use performSelector with three or more arguments?使用带有三个或更多参数的 performSelector?
【发布时间】:2011-12-01 10:38:46
【问题描述】:

各种performSelector:... 方法最多可以处理两个传递给指定选择器的参数。如果我需要传递三个或更多参数,我该怎么办?

【问题讨论】:

  • 是的,如果您可以控制被调用方,最简单的方法是将参数放入 NSDictionary。即使你不“拥有”被调用者,你也可以经常在你自己的类中创建一个“粘合”例程来调用它(假设你正在创建选择器,而不是把它交给你)。否则,您需要 NSInvocation 方法。

标签: objective-c selector performselector


【解决方案1】:

您需要为此使用 NSInvocation 类。请查看this SO question 了解有关使用它们的更多详细信息。

【讨论】:

    【解决方案2】:

    我不喜欢 NSInvocation 方式,它需要太多代码。

    如果您想立即执行选择器,这里有一个简单明了的方法:

    // Assume we have these variables
    id target, SEL aSelector, id parameter1, id parameter2;
    
    // Get the method IMP, method is a function pointer here.
    id (*method)(id, SEL, id, id) = (void *)[vc methodForSelector:aSelector];
    
    // IMP is just a C function, so we can call it directly.
    id returnValue = method(vc, aSelector, parameter1, parameter2);
    

    【讨论】:

      猜你喜欢
      • 2012-08-13
      • 1970-01-01
      • 1970-01-01
      • 2012-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-31
      • 1970-01-01
      相关资源
      最近更新 更多