Re: Why "self"? (Was: Newbie: Referencing Objects)
Re: Why "self"? (Was: Newbie: Referencing Objects)
- Subject: Re: Why "self"? (Was: Newbie: Referencing Objects)
- From: Ken Thomases <email@hidden>
- Date: Mon, 27 Oct 2008 22:06:22 -0500
On Oct 27, 2008, at 9:35 PM, john fogg wrote:
Here is the deal: I create a subobject called "mySubObject" inside my
main object "myMainObject". Now I want to access "myMainObject" from
within "mySubObject".
Your terminology is a bit hard to follow. Part of the problem is that
object's don't have names, they have addresses. Object pointer
variables have names and can contain the addresses of objects. But
the pointer isn't the object and the object isn't the pointer.
Understanding this is important, since you seem to be foggy on how the
two concepts interact. A pointer can change which object it's
pointing to over time. An object may be pointed to by many different
pointers, which will have different names.
And you don't write code or define properties inside objects. You
define them in (or for) classes.
So, when you say "I create a subobject called 'mySubObject' inside my
main object 'myMainObject'" I translate that to mean, that you have a
pointer named myMainObject to an instance of some custom class. In
the implementation of that class, there's a method, and in that method
you create another object of some other(?) class and store its address
into a pointer named mySubObject. mySubObject might be a local
variable, an instance variable, or whatever.
Now, what does "within 'mySubObject'" mean? I assume you mean within
an instance method of the class of which mySubObject is an instance.
OK.
In the method of the first class -- the class of myMainObject; where
you created mySubObject -- you can pass 'self' to mySubObject as a
message parameter. It might be a parameter of the init message (-
initWithOwner:, for example), or you might use a setter (e.g. -
setOwner:). The second class -- the class of mySubObject -- would
keep track of the owner in one of its instance variables, possibly
called "owner". Then, when it wanted to message its owner, it could
do [owner someMethod] or owner.someProperty, etc.
I'm still not sure what the ideal way to do this would be but I have
gotten it to work somehow. Inside "myMainObject" I create a property
and point it to "self" so it stores a reference to "myMainObject".
Is that a typo? You have created a property on an object which refers
to itself? That doesn't make sense to me.
(Again, the property isn't inside myMainObject. It's inside the class
from which myMainObject was instantiated.)
Inside "mySubObject" I cannot access this by writing
[pointerToMainObject doMethod];
but it works when I write
[self.pointerToMainObject doMethod];
Why? What difference does "self" make here?
That doesn't seem sensible to me. However, without seeing the code in
question, it's very hard to tell what might be happening. Seeing the
code would also probably clarify what you're trying to say.
Cheers,
Ken
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden