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

hasankamal007

macrumors newbie
Original poster
Jun 28, 2011
4
0
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
 
Last edited by a moderator:
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.
 
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
 
As far as i anticipated, its just calling dosomething function but, what is this "self".
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.

I was learning from internet and got introduced to this.
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.