Objective C question
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!
Thanks!
If anyone could provide some better explanation or example, it would be much appreciated!
Thanks!
"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?
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?
-- Available Now: Dead Panic, a casual zombie shooter!
-- Development Blog: How to make a game under $1k
-- Twitter: xsmasher
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.
For example, take the class MyObject:
Code:
@interface MyObject
{
int foo;
}
- (void)myMethod;
@endThe implementation of myMethod here
Code:
@implementation MyObject
- (void)myMethod
{
foo += 5;
}
@endis equivalent to this implementation.
Code:
@implementation MyObject
- (void)myMethod
{
self->foo += 5;
}
@endIf you call [myInstance myMethod], it will essentially do myInstance->foo += 5.
That is what is cute about Objective-C. You type the following:
And the compiler sees:
And within the method, self is the receiver.
Code:
[receiver method:argument];And the compiler sees:
Code:
objc_msgSend(receiver, method, argument);And within the method, self is the receiver.
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.
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.
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
!
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
!
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?
Code:
@interface ClassA : NSObject
{
int x;
}
@property int x;
- (void)initVar;
- (void)printVar;
@end
@implementation ClassA
@synthesize x;
- (void)initVar
{
x = 200;
[self printVar];
}
- (void)printVar
{
NSLog(@"%i", self.x);
}
@end
int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
ClassA *myClass = [[ClassA alloc] init];
[myClass initVar];
[pool drain];
return 0;
}which works! But is it feasible, and would this (albeit much more complicated) be WHEN you would use self?
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.
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.
Thanks for the reply, it was very helpful
.
.
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| Objective-C property question. | proxus | 4 | 4,115 |
Feb 26, 2009 10:48 PM Last Post: Josh |
|
| Basic Objective-C question | elliptic | 7 | 4,313 |
Aug 1, 2008 12:03 PM Last Post: SethWillits |
|
| objective-c and c++ question | OptimisticMonkey | 4 | 4,169 |
May 28, 2008 02:51 PM Last Post: OptimisticMonkey |
|
| Beginner - Objective-C/C++/Engines question | LeChuck | 8 | 4,604 |
Nov 26, 2007 04:26 PM Last Post: Duane |
|

