objective-c @selector - 有多个参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2297613/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
@selector - With Multiple Arguments?
提问by fuzzygoat
I have been using @selectortoday for the first time and have not been able to work out how to do the following? How would you write the @selectorif you had more than one argument?
我@selector今天第一次使用,一直无法解决以下问题?@selector如果你有不止一个论点,你会怎么写?
No arguments:
没有论据:
-(void)printText {
NSLog(@"Fish");
}
[self performSelector:@selector(printText) withObject:nil afterDelay:0.25];
Single argument:
单一参数:
-(void)printText:(NSString *)myText {
NSLog(@"Text = %@", myText);
}
[self performSelector:@selector(printText:) withObject:@"Cake" afterDelay:0.25];
Two arguments:
两个论点:
-(void)printText:(NSString *)myText andMore:(NSString *)extraText {
NSLog(@"Text = %@ and %@", myText, extraText);
}
[self performSelector:@selector(printText:andMore:) withObject:@"Cake" withObject:@"Chips"];
Multiple Arguments: (i.e. more than 2)
多个参数:(即超过 2 个)
回答by cobbal
- (id)performSelector:(SEL)aSelector
withObject:(id)anObject
withObject:(id)anotherObject
From the Documentation:
从文档:
This method is the same as performSelector:except that you can supply two arguments for aSelector. aSelector should identify a method that can take two arguments of type id. For methods with other argument types and return values, use NSInvocation.
此方法与performSelector相同:除了您可以为 aSelector 提供两个参数。aSelector 应该标识一个可以接受两个 id 类型参数的方法。对于具有其他参数类型和返回值的方法,请使用NSInvocation。
so in your case you would use:
所以在你的情况下,你会使用:
[self performSelector:@selector(printText:andMore:)
withObject:@"Cake"
withObject:@"More Cake"]
回答by Yoav
As an alternative for NSInvocation when you have more than two parameters, you can use NSObject's -methodForSelector:as in the following example:
当您有两个以上的参数时,作为 NSInvocation 的替代方案,您可以使用 NSObject 的-methodForSelector:如下例所示:
SEL a_selector = ...
Type1 obj1 = ...
Type2 obj2 = ...
Type3 obj3 = ...
typedef void (*MethodType)(id, SEL, Type1, Type2, Type3);
MethodType methodToCall;
methodToCall = (MethodType)[target methodForSelector:a_selector];
methodToCall(target, a_selector, obj1, obj_of_type2, obj_of_type3);
回答by aqua
I had an issue where I needed to use the afterDelayalong with multiple arguments to my @selectormethod. Solution? Use a wrapper function!
我遇到了一个问题,我需要afterDelay在我的@selector方法中使用和多个参数。解决方案?使用包装函数!
Say this is the function I wantto pass to @selector:
说这是我想传递给的函数@selector:
-(void)myFunct:(NSString *)arg1 andArg:(NSString *)arg2 andYetAnotherArg:(NSString *)arg3;
Obviously, I can't even use withObject: withObject:here, so, make a wrapper!
显然,我什至不能withObject: withObject:在这里使用,所以,做一个包装!
-(void)myFunctWrapper:(NSArray *)myArgs {
[self myFunct:[myArgs objectAtIndex:0] andArg:[myArgs objectAtIndex:1] andYetAnotherArg:[myArgs objectAtIndex:2]];
}
and use it by doing:
并通过以下方式使用它:
NSArray *argArray = [NSArray arrayWithObjects:string1,string2,string3,nil];
[self performSelector:@selector(myFunctWrapper:) withObject:argArray afterDelay:1.0];
This way I can have multiple arguments anduse the selector with delay.
这样我就可以有多个参数并延迟使用选择器。
回答by kennytm
@selector(printText:andMore:)
回答by pheelicks
[self performSelector:@selector(printText:andMore) withObject:@"Cake" withObject:@"More Cake"];
回答by Yoav
Another option is to use an even shorter syntax:
另一种选择是使用更短的语法:
#import <objc/message.h> // objc_msgSend
...
((void (*)(id, SEL, Type1, Type2, Type3))objc_msgSend)(target, a_selector, obj1, obj_of_type2, obj_of_type3);
回答by fphilipe
Elaborating on Ben-Uri's answer, which can be written way shorter.
详细说明Ben-Uri 的答案,可以写得更短。
E.g. calling the UIViewmethod - (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)viewcan
be done as follows:
例如调用该UIView方法- (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view可以如下完成:
SEL selector = @selector(covertPoint:toView:);
IMP method = [viewA methodForSelector:selector];
CGPoint pointInB = method(viewA, selector, pointInA, viewB);
回答by VoidPointer
As KennyTM pointed out, the selector syntax is
正如 KennyTM 指出的,选择器语法是
@selector(printText:andMore:)
You call it with
你用
performSelector:withObject:withObject.
... if you need more arguments or different types, you need to use NSIvocation
...如果你需要更多的参数或不同的类型,你需要使用NSIvocation
回答by Kappe
Using NSInvocationas you specify you can create an NSObject category that implements
在指定时使用NSInvocation可以创建一个 NSObject 类别来实现
- (void)performSelector:(SEL)aSelector withObjects:(NSArray *)arguments;
Something like:
就像是:
- (void)performSelector:(SEL)aSelector withObjects:(NSArray *)arguments
{
NSMethodSignature *signature = [self methodSignatureForSelector: aSelector];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature: signature];
[invocation setSelector: aSelector];
int index = 2; //
for (NSObject *argument in arguments) {
[invocation setArgument: &argument atIndex: index];
index ++;
}
[invocation invokeWithTarget: self];
}
from: iOS - How to implement a performSelector with multiple arguments and with afterDelay?

