Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

rstafford84

macrumors newbie
Original poster
Jul 17, 2009
16
0
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?
 
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.
 
may also use self to call a method inside of your current class
[self someMethod: param1];
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.