is this the right way to do a singleton in Objective-C ? (coming from Android background, with a little understanding of threading)
#import "ItemsManager.h"
#define kNumMaxSelectableItems 15
@implementation ItemsManager{
NSMutableArray *currentItems;
}
static ItemsManager *myself;
-(id)init{
if(self=[super init]){
currentItems = [[NSMutableArray alloc] initWithCapacity:kNumMaxSelectableItems];
myself = self;
}
return self;
}
+(ItemsManager *)getInstance{
if(!myself){
myself = [[ItemsManager alloc] init];
}
return myself;
}
-(void) doSomething{
NSLog(@"hi");
}
-(void)dealloc{
[currentItems release];
currentItems = nil;
myself = nil;
[super dealloc];
}
@end
and with this design, i suppose i would call it like
void main(){
[[ItemsManager getInstance] doSomething];
}
***Edit:
how do i hide the init method so that no one else can alloc/init my class without using "getInstance"?