views:

239

answers:

3

How do I access an object that was created in another class. I have one class that constructs a web query goes online and pulls the data back. This results in an object that has all of the information I need. I want to be able to get the state of various variables in that object. The object probably won't change after creation

A: 

Why don't you have a method on the "other" class that constructs the query, pulls the data back and returns the object with the data?

voyager
I can only create the web query once. The data may change and the server is sensitive to the amount of query's.
Rick
@Rick: then you should implement something akin to the singleton pattern on that method http://en.wikipedia.org/wiki/Singleton_pattern
voyager
A: 

I'm not sure what you are asking here as I don't understand your question exactly.
If you mean you are trying to access variables in another object, use the following syntax:

object->varName;

Methods on an object are called using the following syntax:

[object methodName];

You need to know what class an object is an instance of, in order to find out what variables or methods are available to use.

When you want to pass an object back to the function you used to initiate the web query, use:

return object;
nash
I am trying to access variables in an object, but not in the same class as the object was created. I have a class that creates the object with all of the properly formatted info I need. I need to reference that object in different contexts without creating different instances of the empty class. Basically I'm trying to create an info pool that all of the other code in my app can access. I think this is what core data is for, but I'm not quite sure how that is implemented yet.
Rick
`object->varname` only works if the instance variable is defined as `@public`.
Jonathan Sterling
Or if you are accessing it from within the class that owns it.
Jonathan Sterling
A: 

Classes aren't departments; they're the code behind objects. You have information in one object, and another object that needs to access this information.

I think you should look to Cocoa's MVC definition for guidance. Decide which role each of these classes serves. If a class doesn't seem to serve any of them, you probably have some redesigning to do.

Once each class is firmly a Model, View, or Controller, the communication flow between them should be more clear to you.

Peter Hosey