Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a selector from a method name with parameters

I have a code sample that gets a SEL from the current object,

SEL callback = @selector(mymethod:parameter2); 

And I have a method like

 -(void)mymethod:(id)v1 parameter2;(NSString*)v2 { } 

Now I need to move mymethod to another object, say myDelegate.

I have tried:

SEL callback = @selector(myDelegate, mymethod:parameter2); 

but it won't compile.

like image 812
BlueDolphin Avatar asked Nov 18 '08 02:11

BlueDolphin


People also ask

What is a method selector?

A selector is an identifier which represents the name of a method. It is not related to any specific class or method, and can be used to describe a method of any class, whether it is a class or instance method. Simply, a selector is like a key in a dictionary.

How do I create a selector in Swift?

In Swift, Objective-C selectors are represented by the Selector structure, and you create them using the #selector expression. In Swift, you create a selector for an Objective-C method by placing the name of the method within the #selector expression: #selector(MyViewController. tappedButton(_:)) .

How do you call a selector in Swift?

The solution to your problem is to pass the object that should run the selector method along with the selector to the initialisation of the ValueAnimator object. Also update the timerCallback() : @objc func timerCallback() { ... _ = target.


1 Answers

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:

-(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:

@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.

like image 79
Jason Coco Avatar answered Oct 02 '22 22:10

Jason Coco