I have created the following Singleton in my iPhone app:
#import "AppManager.h"
static AppManager *sharedAppManager = nil;
@implementation AppManager
@synthesize currentType, listOfTypes;
#pragma mark Singleton Methods
+ (id)sharedManager {
@synchronized(self) {
if (sharedAppManager == nil)
sharedAppManager = [[self alloc] init];
}
return sharedAppManager;
}
- (id)init {
if (self = [super init]) {
currentType = @"";
listOfTypes = [NSArray arrayWithObjects:@"T1", @"T2", @"T3", nil];
}
return self;
}
- (void)dealloc {
// Should never be called, but just here for clarity really.
[currentType release];
[listOfTypes release];
[super dealloc];
}
@end
When I use it:
// TEST
AppManager *appManager = [AppManager sharedManager];
NSArray *array = appManager.listOfTypes;
NSLog(@"TYPES:%@", array);
I have a strange thing…
TYPES:<UIButtonContent: 0x7b8db10 Title = (null), Image = <UIImage: 0x7bad7f0>, Background = (null), TitleColor = UIDeviceWhiteColorSpace 1 1, ShadowColor = UIDeviceWhiteColorSpace 0 0.5>
It’s working fine with currentType (NSString property) but it does not with listOfTypes (NSArray)
You are not retaining the array listOfTypes, so the array probably got released and is pointing to some garbage value. Use the following to retain the array (if it is a retained/copied property).
You can also simply send the retain message to the array.
or allocate the array.