如何使用带有> 1个参数的选择器调用performSelectorOnMainThread:

dug*_*gla 14 iphone cocoa-touch selector

典型的调用performSelectorOnMainThread:看起来像这样:

[target performSelectorOnMainThread:action withObject:foo waitUntilDone:NO];
Run Code Online (Sandbox Code Playgroud)

其中"result"是传递给"action"的参数.相应的行动将是:

- (void)doSomethingWithThing1:(id *)thing1
Run Code Online (Sandbox Code Playgroud)

调用> 1参数的动作的正确语法是什么?如:

- (void)doSomethingWithThing1:(id *)thing1 andThing2(id *)thing2 andAlsoThing3(id *)thing3

[target performSelectorOnMainThread:action withObject:??? waitUntilDone:NO];
Run Code Online (Sandbox Code Playgroud)

con*_*are 15

您可以将args放在字典或数组中并将其传递给特殊函数

- (void)doStuff:(NSString *)arg1 and:(NSString *)arg2 and:(NSString *)arg3 {
...
}

- (void)doStuff:(NSArray *)argArray {
    [self doStuff:[argArray objectAtIndex:0]
              and:[argArray objectAtIndex:1]
              and:[argArray objectAtIndex:2];
}
Run Code Online (Sandbox Code Playgroud)

  • 它也不适用于粉红色的大象......你有什么意义? (5认同)
  • @GrantlandChew有一个重点 - 你不能天真地使用NSArray,你需要将nil值转换为NSNull.另外我认为粉红色的大象实际上是NSString的子类. (2认同)

Bra*_*son 8

在回答关于将非对象传递给方法的类似问题performSelectorOnMainThread:,我在NSObject上指出了Dave Dribin的类别,它允许您执行以下操作:

[[person dd_invokeOnMainThread] doSomethingWithThing1:thing1 andThing2:thing2 andAlsoThing3:thing3];
Run Code Online (Sandbox Code Playgroud)

用于在主线程上执行多参数方法.我认为这是一个非常优雅的解决方案.在幕后,他将事物包装在NSInvocation中,在主线程上调用它.

琥珀框架做一些与此类似,也是如此.