Objective C question

Apprentice
Posts: 5
Joined: 2010.04
Post: #1
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!
Quote this message in a reply
Member
Posts: 27
Joined: 2008.12
Post: #2
"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?

-- Available Now: Dead Panic, a casual zombie shooter!
-- Development Blog: How to make a game under $1k
-- Twitter: xsmasher
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #3
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:
Code:
@interface MyObject
{
   int foo;
}

- (void)myMethod;
@end

The implementation of myMethod here
Code:
@implementation MyObject
- (void)myMethod
{
   foo += 5;
}
@end

is equivalent to this implementation.

Code:
@implementation MyObject
- (void)myMethod
{
   self->foo += 5;
}
@end

If you call [myInstance myMethod], it will essentially do myInstance->foo += 5.
Quote this message in a reply
Moderator
Posts: 133
Joined: 2008.05
Post: #4
That is what is cute about Objective-C. You type the following:
Code:
[receiver method:argument];

And the compiler sees:
Code:
objc_msgSend(receiver, method, argument);

And within the method, self is the receiver.
Quote this message in a reply
Member
Posts: 227
Joined: 2008.08
Post: #5
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.
Quote this message in a reply
Apprentice
Posts: 5
Joined: 2010.04
Post: #6
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 Smile.

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 Wink!
Quote this message in a reply
Apprentice
Posts: 5
Joined: 2010.04
Post: #7
So to put my thoughts into actual code, I just wrote this:

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?
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #8
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.
Quote this message in a reply
Apprentice
Posts: 5
Joined: 2010.04
Post: #9
Thanks for the reply, it was very helpful Smile.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Objective-C property question. proxus 4 4,777 Feb 26, 2009 10:48 PM
Last Post: Josh
  Basic Objective-C question elliptic 7 5,260 Aug 1, 2008 12:03 PM
Last Post: SethWillits
  objective-c and c++ question OptimisticMonkey 4 4,786 May 28, 2008 02:51 PM
Last Post: OptimisticMonkey
  Beginner - Objective-C/C++/Engines question LeChuck 8 5,431 Nov 26, 2007 04:26 PM
Last Post: Duane