[NSObject init] vs NS_DESIGNATED_INITIALIZER

Just started building my app with 10.11 SDK and am getting a lot of warnings on classes where I have explicitly set NS_DESIGNATED_INITIALIZER where init is not one of them.


E.g


@interface MCChecksumAccumulator : NSObject

typedef NS_OPTIONS(NSInteger, MCWantedChecksums)

{

........

};

/

- (instancetype)initWithWantedChecksums:(MCWantedChecksums)wanted NS_DESIGNATED_INITIALIZER;

@end


@implementation MCChecksumAccumulator <<<< "Method override for the designated initializer of the superclass '-init' not found"

- (instancetype)initWithWantedChecksums:(MCWantedChecksums)wanted

{

...

}

@end


If I remove the NS_DESIGNATED_INITIALIZER from initWithWantedChecksums: then the warning goes away. But I've done this explicitly to prevent people trying to use init on this class directly.


Looking at NSObject I see:


- (instancetype)init

#if NS_ENFORCE_NSOBJECT_DESIGNATED_INITIALIZER

NS_DESIGNATED_INITIALIZER

#endif

;


and NS_ENFORCE_NSOBJECT_DESIGNATED_INITIALIZER is declared as:


/ NS_ENFORCE_NSOBJECT_DESIGNATED_INITIALIZER == 1

* marks -[NSObject init] as a designated initializer. */

#if !defined(NS_ENFORCE_NSOBJECT_DESIGNATED_INITIALIZER)

# define NS_ENFORCE_NSOBJECT_DESIGNATED_INITIALIZER 1

#endif


So it looks as though I can supress this warning by defining NS_ENFORCE_NSOBJECT_DESIGNATED_INITIALIZER to 0 in my prefix file. But that then negates whatever its purpose is.


I could also just define stub init methods in all the affected classes, but thats just adding code that should never be called.


Any ideas of the best way to resolve this?

Your use of NS_DESIGNATED_INITIALIZER changes the rules that the compiler enforces. You can solve this either of 2 ways:


1. Implement init as an actual designated initializer (which means declaring it NS_DESIGNATED_INITIALIZER in an @interface block, though, not the @implementation).

2. Implement init like this:

- (instancetype) init NS_UNAVAILABLE {
     …
}

which is slightly easier.

Either way, the method should throw or assert or otherwise cause a fatal error.

[NSObject init] vs NS_DESIGNATED_INITIALIZER
 
 
Q