0

I've set up a Singleton (with a lot of help from StackOverflow) but when I try to modify/access the array "listOfHeadings", it appears that nothing is changing. I get no errors or warnings from the compiler or when running.

I've got this in my GlobalData.h:

#import <Foundation/Foundation.h>

@interface GlobalData : NSObject {    
    NSMutableArray *listOfHeadings;
}    
@property(nonatomic,retain)NSMutableArray *listOfHeadings;
+(GlobalData*)getInstance;  
@end

This is my GlobalData.m:

#import "GlobalData.h"

@implementation GlobalData
@synthesize listOfHeadings;
static GlobalData *instance; 

+(GlobalData *)getInstance{    
    @synchronized(self){    
        if(!instance){    
            instance= [[GlobalData alloc] init];
            instance.listOfHeadings=[[NSMutableArray alloc]init]; //EDIT: This line added to resolve problem
        }    
    }    
    return instance;    
}    
@end

And I access the Singleton in my AppDelegate.m:

#import "GlobalData.h"
...inside didFinishLaunchingWithOptions...
    GlobalData *globDat=[GlobalData getInstance];
    [globDat.listOfHeadings addObject:@"Message Settings"]; 
    NSLog(@"appdel m array test %i",[globDat.listOfHeadings count]); // prints 0!

So clearly I'm doing something wrong - can some help point out my mistakes? Thanks.

wayneh
  • 4,393
  • 9
  • 35
  • 70

2 Answers2

1

Is listOfHeadings valid and instantiated?

(Note: you CAN call methods on a nil object with no errors in Obj-C!)

MechEthan
  • 5,703
  • 1
  • 35
  • 30
  • Isn't that handled in the GlobalData.h file? – wayneh Jan 19 '12 at 22:14
  • Not in the code you have shown in the question. All you've done is declare that you have a mutable array, you need to actually alloc/init it somewhere. – jrturton Jan 19 '12 at 22:19
  • Got it - I found sample code in other threads...thanks! http://stackoverflow.com/questions/6324732/using-a-singleton-to-create-an-array-accessible-by-multiple-views – wayneh Jan 19 '12 at 22:24
1

You need to have the following:

- (id)init {
    if ( (self = [super init]) ) {
        self.listOfHeadings = [NSMutableArray array];
    }
    return self;
}

- (void)dealloc {
    [listOfHeadings release];

    [super dealloc];
}

Creating the property just makes your listOfHeadings array available to other classes and makes sure it does not get removed. You still need to create the object, which is done in the init method. As the property is retained, you need to release it (note - the one time when self is not needed) to avoid it leaking.

jrtc27
  • 8,496
  • 3
  • 36
  • 68
  • Can someone please fix my formatting - iOS doesn't work well for indenting! – jrtc27 Jan 19 '12 at 22:23
  • Got it - I found sample code in other threads...thanks! http://stackoverflow.com/questions/6324732/using-a-singleton-to-create-an-array-accessible-by-multiple-views – wayneh Jan 19 '12 at 22:25
  • I just edited my original post with the fix from another thread. – wayneh Jan 19 '12 at 22:30
  • Don't alloc/init the array - instead use [NSMutableArray array]; - alloc/init retains the object, and so does your property, but it is only released once (and so leaks), whereas array creates an autoreleased object retained by the property, released once, and doesn't leak. – jrtc27 Jan 19 '12 at 22:33