Creating a selector from a method name with parameters

I have a sample code that gets 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 tried:

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

but it will not compile.

+52
objective-c selector
Nov 18 '08 at 2:25
source share
3 answers

SEL is a type representing a selector in Objective-C. The @selector () keyword returns the SEL you are describing. This is not a function pointer, and you cannot pass any objects or links of any type to it. For each variable in the selector (method), you must present this in a 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 usually passed to delegation methods and callbacks to indicate which method should be called on a particular object during the callback. For example, when creating a timer, the callback method is defined as:

 -(void)someMethod:(NSTimer*)timer; 

Therefore, when you are planning a timer, you should use @selector to indicate which method on your object will 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 indicate that the obj object should be sent with myTimerCallback every 30 seconds.

+93
Nov 18 '08 at 2:48
source share

You cannot pass a parameter to @selector ().

It looks like you are trying to implement a callback. A better way to do this would be something like this:

 [object setCallbackObject:self withSelector:@selector(myMethod:)]; 

Then in your setCallbackObject: withSelector: method: you can call your callback method.

 -(void)setCallbackObject:(id)anObject withSelector:(SEL)selector { [anObject performSelector:selector]; } 
+15
Nov 18 '08 at 2:34
source share

In addition to what has been said about selectors, you can look at the NSInvocation class.

NSInvocation is an Objective-C message that displays as static, that is, this action is turned into an object. NSInvocation objects are used to store and forward messages between objects and between applications, mainly NSTimer objects and the distributed object system.

An NSInvocation object contains all the elements of an Objective-C message: target, selector, arguments, and return value. Each of these elements can be set directly, and the return value is set automatically when the NSInvocation object is sent.

Keep in mind that while this is useful in certain situations, you are not using NSInvocation on a normal coding day. If you are just trying to get two objects to talk to each other, consider defining an informal or formal delegate protocol, or pass in a selector and a target, as mentioned.

+5
Nov 18 '08 at 4:11
source share



All Articles