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:);
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.
Jason Coco Nov 18 '08 at 2:48 2008-11-18 02:48
source share