PDA

View Full Version : Irritated with Program




zcarter
Mar 24, 2008, 05:01 AM
I am so irritated right now! Lol.

I have 3 classes. My first class, (AppDelegate) has an NSMutableArray called list.

This 'list' is very important as it is needed in not only the 2nd class but the 3rd class as well.

The problem is..the 2nd class calls the first class making its own instance of the first class.

The 3rd class...also calls the 1st class, making ITS own instance of the first class.

I need the 2nd and 3rd class to have the SAME instance of the first class..how can I do that?

Thanks,

- Zac



toddburch
Mar 24, 2008, 08:06 AM
First, I don't know a thing about Objective-C.

But, I do know a bit about OO programming.

It sounds as if you set up for first class to return a new instance of itself every time it is called, but it sounds like you don't what an instance, but rather you want a Singleton class, where each time you call your first class, it returns the one and only array you need to share.

Each language has its own syntax for do this, and I apologize for not being able to tell you what it is in Obj-C.

Todd

kainjow
Mar 24, 2008, 08:07 AM
How are these classes related? Where are they created?

Usually in these types of situations you have one class that handles the data and the controller classes use a shared instance of the data class, or you pass around parts of the data to the other classes via accessors/properties.

Sayer
Mar 24, 2008, 08:27 AM
Do something like this in the class you want to share:

@implementation MyMIDIClient

static MyMIDIClient *instance = nil;

- (MyMIDIClient *)init {
if ([super init]) {
if (MIDIClientCreate(CFSTR("MacAdShow"), myMIDINotifyProc, NULL, &myMIDIClient) != noErr)
return nil;
{
OSStatus result;

result = MIDIInputPortCreate(myMIDIClient, CFSTR("Input port"), MyReadProc, NULL, &inPort);

}

NSNumber *tmpMinVal = [[[NSUserDefaultsController sharedUserDefaultsController] values] valueForKey:@"midiVelocityMin"];

midiVelocityMin = [tmpMinVal intValue];

NSNumber *tmpMaxVal = [[[NSUserDefaultsController sharedUserDefaultsController] values] valueForKey:@"midiVelocityMax"];

midiVelocityMax = [tmpMaxVal intValue];

} else {

midiVelocityMin = 10; // floor for proximity switch
midiVelocityMax = 40; // ceiling for proximity switch

return nil;

}

return self;
}

+ (MyMIDIClient *)sharedInstance
{

if (!instance)
instance = [[self alloc] init];

return instance;
}

...

@end

Make your init like usual, but have a global static variable that will hold the singular copy of the shared class. Then in the [class sharedInstance] method (note the plus sign) you alloc/init to the global static and then pass it back only if the global is undefined.

Then in each class that wants to use the shared instance just do the following:

NSArray *endpoints = [[MyMIDIClient sharedInstance] allDestinationEndpoints];

If the shared instance was not yet allocated, this will do so only once, otherwise it passes back the same singular instance of the shared class for you to access as needed.

kainjow
Mar 24, 2008, 09:27 AM
I prefer putting the static variable inside the class method. Makes for cleaner code:

+ (MyMIDIClient *)sharedInstance
{
static MyMIDIClient *instance = nil;
if (!instance)
instance = [[self alloc] init];

return instance;
}