objective-c 从带有参数的方法名称创建选择器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/297680/
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
Creating a selector from a method name with parameters
提问by BlueDolphin
I have a code sample that gets a SELfrom the current object,
我有一个SEL从当前对象中获取 a 的代码示例,
SEL callback = @selector(mymethod:parameter2);
And I have a method like
我有一个像
-(void)mymethod:(id)v1 parameter2;(NSString*)v2 {
}
Now I need to move mymethodto another object, say myDelegate.
现在我需要移动mymethod到另一个对象,比如myDelegate。
I have tried:
我试过了:
SEL callback = @selector(myDelegate, mymethod:parameter2);
but it won't compile.
但它不会编译。
回答by Jason Coco
SEL is a type that represents a selector in Objective-C. The @selector() keyword returns a SEL that you describe. It's not a function pointer and you can't pass it any objects or references of any kind. For each variable in the selector (method), you have to represent that in the call to @selector. For example:
SEL 是一种代表 Objective-C 中选择器的类型。@selector() 关键字返回您描述的 SEL。它不是函数指针,您不能将任何对象或任何类型的引用传递给它。对于选择器(方法)中的每个变量,您必须在对@selector 的调用中表示该变量。例如:
-(void)methodWithNoParameters;
SEL noParameterSelector = @selector(methodWithNoParameters);
-(void)methodWithOneParameter:(id)parameter;
SEL oneParameterSelector = @selector(methodWithOneParameter:); // notice the colon here
-(void)methodWIthTwoParameters:(id)parameterOne and:(id)parameterTwo;
SEL twoParameterSelector = @selector(methodWithTwoParameters:and:); // notice the parameter names are omitted
Selectors are generally passed to delegate methods and to callbacks to specify which method should be called on a specific object during a callback. For instance, when you create a timer, the callback method is specifically defined as:
选择器通常传递给委托方法和回调,以指定在回调期间应在特定对象上调用哪个方法。例如,当你创建一个定时器时,回调方法具体定义为:
-(void)someMethod:(NSTimer*)timer;
So when you schedule the timer you would use @selector to specify which method on your object will actually be responsible for the callback:
因此,当您安排计时器时,您将使用 @selector 指定对象上的哪个方法将实际负责回调:
@implementation MyObject
-(void)myTimerCallback:(NSTimer*)timer
{
// do some computations
if( timerShouldEnd ) {
[timer invalidate];
}
}
@end
// ...
int main(int argc, const char **argv)
{
// do setup stuff
MyObject* obj = [[MyObject alloc] init];
SEL mySelector = @selector(myTimerCallback:);
[NSTimer scheduledTimerWithTimeInterval:30.0 target:obj selector:mySelector userInfo:nil repeats:YES];
// do some tear-down
return 0;
}
In this case you are specifying that the object obj be messaged with myTimerCallback every 30 seconds.
在这种情况下,您指定对象 obj 每 30 秒使用 myTimerCallback 发送消息。
回答by Grant Limberg
You can't pass a parameter in a @selector().
你不能在@selector() 中传递参数。
It looks like you're trying to implement a callback. The best way to do that would be something like this:
看起来您正在尝试实现回调。最好的方法是这样的:
[object setCallbackObject:self withSelector:@selector(myMethod:)];
Then in your object's setCallbackObject:withSelector: method: you can call your callback method.
然后在您对象的 setCallbackObject:withSelector: 方法中:您可以调用您的回调方法。
-(void)setCallbackObject:(id)anObject withSelector:(SEL)selector {
[anObject performSelector:selector];
}
回答by Marc Charbonneau
Beyond what's been said already about selectors, you may want to look at the NSInvocation class.
除了已经说过的关于选择器的内容之外,您可能还想看看 NSInvocation 类。
An NSInvocation is an Objective-C message rendered static, that is, it is an action turned into an object. NSInvocation objects are used to store and forward messages between objects and between applications, primarily by NSTimer objects and the distributed objects system.
An NSInvocation object contains all the elements of an Objective-C message: a target, a selector, arguments, and the return value. Each of these elements can be set directly, and the return value is set automatically when the NSInvocation object is dispatched.
NSInvocation 是一个静态渲染的Objective-C 消息,也就是说,它是一个动作变成了一个对象。NSInvocation 对象用于在对象之间和应用程序之间存储和转发消息,主要是通过 NSTimer 对象和分布式对象系统。
NSInvocation 对象包含Objective-C 消息的所有元素:目标、选择器、参数和返回值。这些元素中的每一个都可以直接设置,并且在调度 NSInvocation 对象时自动设置返回值。
Keep in mind that while it's useful in certain situations, you don't use NSInvocation in a normal day of coding. If you're just trying to get two objects to talk to each other, consider defining an informal or formal delegate protocol, or passing a selector and target object as has already been mentioned.
请记住,虽然它在某些情况下很有用,但您不会在正常的编码日中使用 NSInvocation。如果您只是想让两个对象相互通信,请考虑定义一个非正式或正式的委托协议,或者如前所述传递选择器和目标对象。

