Irritated with Program

Discussion in 'Mac Programming' started by zcarter, Mar 24, 2008.

  1. macrumors member

    Joined:
    Apr 21, 2007
    #1
    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
     
  2. macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #2
    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
     
  3. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #3
    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.
     
  4. macrumors 6502a

    Sayer

    Joined:
    Jan 4, 2002
    Location:
    Austin, TX
    #4
    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.
     
  5. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #5
    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;
    }
     

Share This Page