Take the 2-minute tour ×

Possible Duplicate:
Objective-C - When to use ‘self’

I can't understand very well the importance and the usage of the "self" keyword in objective-c. It's my first OOP language so i got stuck on some concepts. When i should use "self"? Why is it useful?

Thanks for the answers!

Edit: I can't understand why is this a duplicated post of Objective-C - When to use 'self' when there there is not the explanation of "Self" that i wanted.

share|improve this question
No, i saw that post :) –  codeluca Jan 26 '13 at 9:25
Well then why in the programmer's girlfriend are you re-posting it? It gives step to step, nicely detailed explanation. Stop reposting questions on SO –  MCKapur Jan 26 '13 at 9:28
I'm not reposting questions. I read the post that you linked but I have not figured it out yet. –  codeluca Jan 26 '13 at 9:35
add comment

marked as duplicate by MCKapur, msgambel, Mitch Wheat, Carl Veazey, wattson12 Jan 26 '13 at 9:37


1 Answer

self is a special variable in Objective-C, inside an instance method this variable refers to the receiver(object) of the message that invoked the method, while in a class method self will indicate which class is calling.

self refers to the actual object that is executing the current method, it is an invisible argument passed automatically by the runtime environment to your instance methods.

share|improve this answer
For both instance and class methods, it "refers to the receiver of the message". It's just that in a class method, the receiver is a class object. Also, it only refers to that at the beginning of the method -- self can be re-assigned like any other local variable. –  newacct Jan 26 '13 at 20:19
add comment
