So I am writing a singleton object and want to mark the init method as NS_UNAVAILABLE
or
__attribute__((unavailable("Use 'sharedInstance' instead of 'init' as this class is singleton.")));
from Safe way to create singleton with init method in Objective-C second answer.
The question is for the second answer, it does not work:
However, Xcode 7.3 prompts me a compiler error in the singleton implementation:
@interface NetWorkService : NSObject
+(nonnull instancetype)sharedInstance;
-(nonnull instancetype)init NS_UNAVAILABLE;
@end
@implementation NetWorkService
+(instancetype)sharedInstance {
static NetWorkService *sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[NetWorkService alloc] init]; <--- init is unavailable
});
return sharedInstance;
}
@end
Is this a bug or what I missed? Thanks.