Objective C: Please explain SEL and selector

Posted on 2012-08-14
Medium Priority
Last Modified: 2016-02-10
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;
Question by:newbieweb
LVL 15

Assisted Solution

by:Hamidreza Vakilian
Hamidreza Vakilian earned 600 total points
ID: 38304577
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.
LVL 33

Accepted Solution

pgnatyuk earned 800 total points
ID: 38308934
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:
LVL 12

Assisted Solution

satsumo earned 600 total points
ID: 38309987
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.

Author Closing Comment

ID: 38310047

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What's a UDID? If you're involved in developing, testing, or even reviewing an iOS application that's in beta, then at some point you may need to know the UDID for any iOS devices that you'll be testing on. What's the UDID? It stands for Unique Dev…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question