one third party library uses my class initialisation:
ClassA *a = [[MyClass alloc] init]];
I need MyClass
to be shared instance (aka singleton) but I can't modify 3rd party way of executing MyClass
initialization
I was trying to override init
method as following:
- (instancetype)init
{
return [[self class] sharedInstance];
}
+ (LoopMeNativeEvent *)sharedInstance
{
static LoopMeNativeEvent *_sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_sharedInstance = [LoopMeNativeEvent new];
});
return _sharedInstance;
}
but, unfortunately new
causes alloc init
to be executed.
Simplest way I know is to have two separate classes:
MyClass
which will be initialised throughalloc init
- Separate
MySharedClass
which is singleton
Is there possibility achieve this having just one class?