• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1346
  • Last Modified:

Objective C: Please explain SEL and selector

I am a former C++ developer and currently C#, but this Objective C is not at all clear to me.

I read up on it and it became less clear.  Please explain SEL and selector.


Feel free to use (or not use) my sample source code:

  //notify the delegate if steps are completed
    if (_priorStepCompleted == _numSteps) {
        if([self.delegate respondsToSelector:@selector(IsFinished)]){
            [self.delegate performSelector:@selector(IsFinished)];

        return YES;
    return NO;
3 Solutions
Hamidreza VakilianSenior iOS DeveloperCommented:
Generally speaking, a selector is a pointer to a function. We declare a selector with @selector() command and also we can store it in a variable as follows:
SEL mySelector = @selector(myDesiredFunction);
Now you would ask what is the pros of pointing to a function?
- The big pro is that we can pass selectors to functions! As you know we can pass all kinds of data structures to functions, but what if we want to pass a function as an argument to another function? thats why we use selectors.

About your sample code:
- respondsToSelector is a function which takes a SEL as its argument then it checks whether that function is defined in the corresponding class or not.
- performSelector is a function which takes a SEL as its first arg; it calls the function which you defined the selector with. this method has other overrides too, such as the one which takes a second arg of type object, which it passes to the target function.
In the sample code provided, the programmer is not sure about the existance of isFinished function in self.delegate class; so to prevent from possible runtime error, he first checks the existance of isFinished with respondsToSelector then if the conditions meets it will be called through the performSelector function.
I'd say, it's a mistake to think that the selector in Objective-C is a pointer to a function.

The selector identifies a message that is sent to an object. SEL is a data type used to declare the selectors.

Please read in iOS Developer Library:
satsumoSoftware DeveloperCommented:
One of the important distinctions between a function pointer and a selector is that a selector is dynamic.  You can use the selector on any object that has that method.  You can ask an object (or a class) if it supports a selector.  I sometimes think of them as being similar to function names, where you can say "call the method named 'doThing' on this object".  You could also call 'doThing' on an object of a different class, as long as it has that method.

The closest thing in C++ is a virtual pure function, but even that is more restrictive because it has to be declared before hand.  A selector can be applied to any object, even if that method is not in its interface definition.  Unlike C++ you can add methods to an Objective-C class without declaring them.

@selector defines a selector object, SEL is a pointer to a selector object.  Much like @"string" defines an object of type NSString.  Selectors do not imply anything about parameters.

The only language I know that does something similar is Javascript, where all functions are named with strings and there are no defined classes as such.
newbiewebSr. Software EngineerAuthor Commented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now