Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

zcarter

macrumors member
Original poster
Apr 21, 2007
46
0
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

macrumors 6502a
Dec 4, 2006
748
0
Katy, Texas
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

Moderator emeritus
Jun 15, 2000
7,958
7
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

macrumors 6502a
Jan 4, 2002
981
0
Austin, TX
Do something like this in the class you want to share:

Code:
@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:

Code:
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

Moderator emeritus
Jun 15, 2000
7,958
7
I prefer putting the static variable inside the class method. Makes for cleaner code:

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

  return instance;
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.