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

    Joined:
    Jun 28, 2011
    #1
    hi,
    can anyone tell me what the following code means:
    Code:
    [self dosomething];
    here, dosomething is a function declared as:
    Code:
    -(void)dosomething;
    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".

    Regards,
    Hasan
     
  2. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #2
    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

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #3
    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

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    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:
    Code:
    [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:
    Code:
    [doSomething];
    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