Below is the code in my singleton to add a string to an array. The first NSLog returns the correct string but the second NSLog does not and returns null. If there is any relevant code I need to add let me know. Thanks.
+(void) addArray:(NSString *)string
{
NSLog(@"%@",string);
Singleton *shared = [Singleton sharedManager];
[shared.array addObject:string];
NSLog(@"array: %@", shared.array);
}
I added this code to my singleton.m
- (id)init
{
self = [super init];
if (self) {
// Work your initialising magic here as you normally would
array = [[NSMutableArray alloc]init];
}
return self;
}
In another part of my program I run this code and it prints out the correct information
+(void) setPoints:(int) points
{
Singleton *shared = [Singleton sharedManager];
shared.points = points;
NSLog(@"%d",shared.points);
}
Here is my Singleton method
+ (Singleton *)sharedManager
{
static Singleton *sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[Singleton alloc] init];
// Do any other initialisation stuff here
array = [[NSMutableArray alloc]init];
});
return sharedInstance;
}
It’s possible that you forgot to alloc-init array in your Singleton init.