Objective C: Please explain SEL and selector

Posted on 2012-08-14
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 14

    Assisted Solution

    by:Hamidreza Vakilian
    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

    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

    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


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
    In this article I'm going to cover the basics and usages of NSInvocation. What is NSInvocation? Apple Developer Reference describes NSInvocation this way: An NSInvocation is an Objective-C message rendered static, that is, it is an action tur…
    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.

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now