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.