Objective C question - Printable Version
+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Programming Languages & Scripting (/forum-8.html)
+--- Thread: Objective C question (/thread-168.html)
Objective C question - Wayrath - Apr 10, 2010 06:24 PM
Hey, I'm working my way through Programming in Objective-C 2.0 2nd Edition right now. It's going very well, but I find that I am getting stuck trying to understand what exactly "self" is. Conceptually I'm just not getting it.
If anyone could provide some better explanation or example, it would be much appreciated!
Objective C question - smasher - Apr 10, 2010 10:35 PM
"self" is an pointer to the current object - the current instance. If I want to send a message to a different object I might call [thatObject someMethod], but if I want to send a message to myself I call [self someMethod] .
Is that what you wanted, or did you have a specific question about the way self is used in an init method or with properties?
Objective C question - akb825 - Apr 10, 2010 11:37 PM
As I'm sure you've learned by now, an object is a collection of functions and variables. Whenever you send a message to an object, self is always the object that the message was sent to. So in the case of the example [thatObject someMethod], self is the value of thatObject. When you access any member variables, it's implicitly grabbing those from self as well. In other words, if you have the member variable foo, whenever you type foo in a member function it's equivalent to typing self->foo.
For example, take the class MyObject:
The implementation of myMethod here
is equivalent to this implementation.
If you call [myInstance myMethod], it will essentially do myInstance->foo += 5.
Objective C question - longjumper - Apr 11, 2010 01:07 AM
That is what is cute about Objective-C. You type the following:
And the compiler sees:
And within the method, self is the receiver.
Objective C question - Oddity007 - Apr 11, 2010 06:54 AM
If it helps, self is an implicit argument that points to the object that you sent the message to.
It typically is the first argument, you don't declare it, it's done for you.
I would recommend looking at how Objective-C is converted to C.
Objective C question - Wayrath - Apr 11, 2010 08:40 AM
Thank you all for the responses!
I am slowly starting to wrap my mind around it, for some reason this in particular seems strange to me .
So to see if I'm understanding it right: let's say I had an instance of a class ClassA, myClass. in ClassA is the function initVar which sets an int to 200. So if I call [myClass initVar], any reference to "self" in the function initVar would be referring to myClass as it is the object that initVar is referring to in this case?
If this is correct, using this example, what would a practical use of the keyword self be?
I think (hope) i'm starting to understand what self refers to, but I'm having trouble seeing when it would be used.
Here's my guess: That inside the function initVar, if for some reason I was to call another function, let's say printVar. Then I could pass [self printvar] within initVar, in equivalence of saying [myClass printvar] back in main() or where ever the main program was.
Thanks again for your quick responses, and pardon my dust in learning the ropes !
Objective C question - Wayrath - Apr 11, 2010 08:55 AM
So to put my thoughts into actual code, I just wrote this:
which works! But is it feasible, and would this (albeit much more complicated) be WHEN you would use self?
Objective C question - akb825 - Apr 11, 2010 11:38 AM
The two most common uses of self are if you want to send another message to that object or pass that object as a parameter to another object's message.
You already cover the first use with your own example. It becomes very useful as you have much more complicated operations on your class, so you don't have to copy and paste large chunks of code within multiple message implementations, which would become unfeasible and unmaintainable very quickly.
Here are two common cases where you want to send another message to the same object. One is if you want to do operation A, which also needs to perform operation B, but you can also do operation B by itself. For example, say you have a Car object. You can turn on the car, and turn on the radio. You can turn on the radio by itself, but when you turn on the car it will also automatically turn on the radio. Another common case is if you find you're doing the same thing over and over within multiple messages. In this case, you can pull out the common code into its own private message, and call that from each of the messages you need to do that operation.
The second use case for self would look like this: [otherObject doSomething:self]. In this case, otherObject needs to do an operation that relies on the current object, so you pass self in as a parameter. In the example of a Car class, say you have a security system on your car, represented as a securityService member variable. If it detects it's being stolen, it can send the message [securityService helpMe:self].
If you want to make good decisions on how to make your objects, you should get a book on object oriented design patterns. Since it sounds like Objective C is your first object oriented language, and you're concentrating on Cocoa anyway, you should probably check out Cocoa Design Patterns. As soon as you feel comfortable on the concepts of what an object is, and how they can interact with each other, knowing what "good design" is will allow you to make larger systems that don't collapse on themselves.
Objective C question - Wayrath - Apr 15, 2010 07:25 AM
Thanks for the reply, it was very helpful .