In your opinion if I have a singleton subclass of NSObject being initialised with parameters like this:
- (MyObject *) initWithSomeParam:(NSString *)param{
self = [super init];
if (SharedInstance == nil){
SharedInstance = [super init];
SharedInstance.someProperty = param;
}
return self;
}
+ (MyObject *) objectWithSomeParam:(NSString *)param{
return [[self alloc] initWithSomeParam:param];
// Will the alloc cause a leak?
}
The user doesn’t have access to the instance method, just the class. Thanks.
That’s not the normal way of implementing a singleton and you are breaking the convention of
init. Better would be to create asharedInstanceclass method and leave theinitWithParammethod to be more conventional:However, even that doesn’t seem very comfortable; i.e. what happens if the user calls
sharedInstancewith a different parameter? Perhaps you want to keep aNSMutableDictionaryof the initialized objects and create/return them depending on the parameter?If so, you would do: