Re: Communication between objects
Re: Communication between objects
- Subject: Re: Communication between objects
- From: "I. Savant" <email@hidden>
- Date: Wed, 11 Feb 2009 10:01:35 -0500
> Say I have myControllerObject. I alloc and init modelObject1 and also
> modelObject2.
> How do I access the ivars of modelObject1 from modelObject2 and vice versa?
> modelObject1 and modelObject 2 both exist somewhere in memory, so how do I
> get a pointer to or make a connection between these two object?
This is more of an Object Oriented Programming question in general
and you'll probably do better to look for answers in places that cover
this topic, rather than Cocoa specifically. However ...
You need to think about pointers. Say your ModelObject is a person.
You designed it to have a "firstName" and a "lastName" property. The
iVars would be:
NSString * firstName;
NSString * lastName;
... you'd have accessors so that other objects can ask:
[modelObject1 lastName] ... and so on. These (first/last name
ivars) are pointers to other objects (strings).
So if you understand that, why the struggle with the idea of having
a pointer to *another* object you want to get to? If your controller
needs to access a model object, it needs a pointer to get to it. In
your case, it sounds like your controller will be managing multiple
"ModelObject" instances, right? So it should have a container in which
to keep them. In that case, your controller might have an NSArray ivar
called "modelObjects" or something a little more descriptive. Your
controller can then get at the individual model objects by asking its
modelObjects array for an object at a certain index.
That is only one example of one scenario. The best course of action
(ie, the best application architecture) depends on a combination of
your app's purpose and the API's design pattern (in Cocoa's case, the
Model / View / Controller design pattern, which is a common design
pattern and is something YOU MUST LEARN).
In the case of your model objects speaking to one-another, the
answer is the same: your model object needs a property (the
combination of an iVar with accessors that other objects can use to
set/get the property). The property would be a ModelObject pointer.
> Also, how do I access the ivars of myControllerObject from either
> modelObject1 or modelObject2?
In general terms, you *shouldn't* do this. If you're using Cocoa,
you need to be following the MVC design pattern. According to MVC,
your model layer should know nothing of your controller (or view)
layer. Having a model object manipulating a controller object is
backwards. Your controller can "observe" your model for changes (ie,
Notifications or Key Value Observing) and react to this, but your
model objects should not be actively manipulating (sending messages to
or querying) any controllers.
> Is it possible to find out from myObject1 or myObject2 who init and alloc
> them?
This is quite simply the wrong approach. Forget about this and learn
the Model View Controller design pattern:
http://developer.apple.com/documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaDesignPatterns/chapter_5_section_4.html
... learn it NOW before you waste any more time stumbling about in
the dark nowhere near the right neighborhood. :-)
> From what I've read to far (Objective C-2.0 Programming Language and Cocoa
> Fundamentals Guide) I've been unable to find the answer to these questions.
Read them again.
> I can send messages from myControllerObject to myObject1 or myObject2 and
> get returned information no worries, that I do get.
Then you get the concept of pointers and sending messages to objects
*via* pointers, but you're lacking the architectural part of this (a
large, OH-MY-GOD-YOU-SHOULD-NOT-PROCEED-UNTIL-YOU-GET-THIS part).
--
I.S.
_______________________________________________
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