My question is quite similar to this one: Use Singleton In Interface Builder?
The only difference is that I use ARC. So, if simplified, my singleton looks like that:
Manager.m
@implementation Manager
+ (instancetype)sharedManager {
__strong static id sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}
@end
So the question is if it’s possible to adopt it for Interface Builder still being with ARC?
Of course, I understand that it might be simpler just to rewrite that class without ARC so the question is rather academic. 🙂
When the nib is unarchived, it’ll attempt to either
alloc/initoralloc/initWithCoder:a new instance of the class.So, what you could do is intercept that call and re-route it to return your singleton:
This allows
-initand-initWithCoder:to be safely called multiple times on the same object. It’s generally not recommended to allow this, but given that singletons are already cases of “a place where things can get really wonky”, this isn’t the worst you could do.