Re: Global variables?
Re: Global variables?
- Subject: Re: Global variables?
- From: Matt Reagan <email@hidden>
- Date: Tue, 27 Dec 2005 03:42:24 -0800
I actually tried doing that- encompassing the data inside its own
object. But the problem I'm having involves how to get the data when I
need it.
I have a controller object that does much of the processing and
coordinating for the program, its much of the 'guts' of the simple
program, and it is also responsible for init'ing the single instance
of this data object.
That's fine and dandy- the controller can then access the integers via
the data object's accessor methods. The problem is that I have a
custom NSView (among other things) that needs to also access this
data. Since the controller creates the instance of the data object,
it's the only object that has a reference pointing to the data object,
so any other classes doesn't know how to find the data object to
request the integers. I suppose I could create a method in the
controller that would allow me to do this:
int x;
x = [[myController dataObj] giveMeTheXInteger];
But this roundabout way of referencing the controller, which
references the data object, which then returns its integer, seems
really silly.... it's just an integer. I just need multiple classes to
be able to modify and access it.
Maybe I'm just not understanding how this works, I apologize, I'm a
newbie. I'm not fully understanding how all these objects are supposed
to find each other. Objective C seems like an efficient language, I
can't believe I would have to create an instance variable in every
single class just to point to this data object... will I? Thank you
for your feedback, any additional help in explaining this will be
greatly appreciated.
-matt
On 12/27/05, j o a r <email@hidden> wrote:
>
>
On 27 dec 2005, at 11.55, Matt Reagan wrote:
>
>
> I understand that in OOP it's generally said to be a bad idea to use
>
> 'global' variables outside the normal object-oriented methodology...
>
> but can anyone please advise me on the best way to make a very small
>
> array of integers available to my entire application? I have a main
>
> controller object that had this:
>
>
>
> @public
>
> int myIntArray[10];
>
>
>
> But despite the public command, I can't seem to access it from another
>
> class. I made sure to include the controller's header file where it's
>
> needed.
>
>
>
> What's the easiest way to make some integers available to the whole
>
> application?
>
>
Easiest or best?
>
>
In OOP you have objects, not free floating data. Objects holds data,
>
and methods that acts on their data. So, who is the "owner" of your
>
int array? You can probably build a class to hold that data, in which
>
case you would allow access to that data via methods on that class:
>
>
@class MyIntArray : NSObject
>
{
>
@private
>
>
int myIntArray[10];
>
}
>
>
- (int) intAtIndex:(int) index;
>
- (void) setInt:(int) integer atIndex:(int) index
>
>
@end
>
>
You might want to consider implementing this class as a singleton class.
>
>
j o a r
>
>
>
>
>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden