1

I have a header for an object that has new marked as unavailable and that is correct (as it should be a singleton)

@interface Manager : NSObject <NSCopying>

+ (instancetype)new NS_UNAVAILABLE;

- (instancetype)init NS_UNAVAILABLE;

+ (instancetype)sharedInstance;

@end

....... now this is correct for the outside but the @implementation of Manager itself should be exempt from this .... I want it to be able to call [self new]

e.g. I want

@implementation Manager 

+ (instancetype)shared {
    static id shared = nil;
    if(!shared) {
        shared = [self new];
    }
    return shared;
}
@end

Please note this is an example and this NOT about singletons. I want to call new even though it is forbidden and above was an example

possible solutions:

  1. so can I somehow override the unavailability attribute? (preferred)
  2. is it safe to just call [super new]
Community
  • 1
  • 1
Daij-Djan
  • 49,552
  • 17
  • 113
  • 135
  • this is completely different than what matt told me is a duplicate: https://stackoverflow.com/questions/7274360/how-objective-c-singleton-should-implement-init-method – Daij-Djan Feb 03 '18 at 23:26
  • I know how to implement a singleton - the title IS important, singleton is a sample. – Daij-Djan Feb 03 '18 at 23:27

1 Answers1

1

You can cast to id, then you can send the message new without problems:

shared = [(id)self new];

You can also use [super new] if your class has no implementation of new.

Tammo Freese
  • 10,514
  • 2
  • 35
  • 44