Use of Keyword "Self"

Discussion in 'iOS Programming' started by rstafford84, Aug 13, 2009.

  1. rstafford84 macrumors newbie

    Joined:
    Jul 17, 2009
    #1
    At what times should I use the keyword self in Objective-C? Sometimes it suffices to do [variable action] while other times it requires me to do [self.variable action]. Is there a hard-fast rule concerning this?
     
  2. drivefast macrumors regular

    Joined:
    Mar 13, 2008
    #2
    self.variable will always invoke the getter function of the member "variable", whereas calling it simply variable will just use the variable itself. since most of the time people use @synthesize, they wont explicitly see the getter and the setter functions, so in this case i would say use the variable name without prefixing with self. when you will need to do more than just setting a variable value in the setter, and you will give up the synthesized function and write your own, you will figure out what i mean.
     
  3. Kingbombs macrumors member

    Joined:
    Jun 24, 2009
    #3
    may also use self to call a method inside of your current class
    [self someMethod: param1];
     

Share This Page