what is [self dosomething] in objective-c?

Discussion in 'iOS Programming' started by hasankamal007, Jun 29, 2011.

  1. hasankamal007, Jun 29, 2011
    Last edited by a moderator: Jun 29, 2011

    hasankamal007 macrumors newbie

    Jun 28, 2011
    can anyone tell me what the following code means:
    [self dosomething];
    here, dosomething is a function declared as:
    I was learning from internet and got introduced to this.
    As far as i anticipated, its just calling dosomething function but, what is this "self".

  2. jnoxx macrumors 65816


    Dec 29, 2010
    Aartselaar // Antwerp // Belgium
    Have u thought about reading some books about Objective C, before jumping into this language.
    Because what does SELF mean, (the world itself in the English language).
    I thought that should explain alot. because you are referring to yourself = self.
    So your calling the function on yourself.
    That's why if your calling a function from something else u don't say [self do something]; but [otherThing dosomething].

    Dno how to explain it better. Maybe Dejo/Robbie can give it a go. But they will advice u to read up on a book or something similair, so I'm just saving u for them.
  3. robbieduncan Moderator emeritus


    Jul 24, 2002
    self is a pointer to the "current" object. I.e. the object context the method is currently executing within.

    I'm honestly not sure that is better :p
  4. dejo Moderator


    Staff Member

    Sep 2, 2004
    The Centennial State
    When we use the common expression of "calling a method", Objective-C, in fact, is "sending a message to a receiver". So, you can think of it as:
    [receiver message];
    Often, the receiver is some other instance or class that we wish to send the message to. But, frequently, we want to call the method for the current instance of the class. We can't simply call:
    because of the nature of Objective-C: all messages must specify a receiver; therefore, the self keyword can be used to supply a receiver when you mean the instance of the class which the code is currently executing within. Hope that helps.

    What resources are you using to learn Objective-C? Please be specific. Provide URLS, etc. This will help us to understand what you're being taught out there.

Share This Page