Re: New error “Method override for the designated initializer of the superclass ‘-init’ not found”
Re: New error “Method override for the designated initializer of the superclass ‘-init’ not found”
- Subject: Re: New error “Method override for the designated initializer of the superclass ‘-init’ not found”
- From: Kyle Sluder <email@hidden>
- Date: Wed, 10 Jun 2015 02:16:04 -0500
On Tue, Jun 9, 2015, at 03:40 PM, Jens Alfke wrote:
> I’m getting a couple of odd new compile errors with Xcode 7. These are in
> classes that subclass NSObject and add an initializer that’s declared as
> a designated initializer, but do not implement -init because it’s not
> valid to initialize them without parameters. The new error says “Method
> override for the designated initializer of the superclass ‘-init’ not
> found”.
>
> @interface Flump : NSObject
> - (instancetype) initWithString: (NSString*)str
> NS_DESIGNATED_INITIALIZER;
> @end
>
> @implementation Flump // <- error reported on this line
> - (instancetype)initWithString: (NSString*)str {
> self = [super init];
> if (self) {
> //...
> }
> return self;
> }
> @end
>
> I suppose the error points out that someone initializing a Flump with
> -init would call the inherited -init from NSObject without any
> Flump-specific initialization occurring. Which seems sort of valid. So to
> fix the error I added an override of -init:
>
> - (instancetype) init {
> @throw [NSException exceptionWithName:
> NSInternalInconsistencyException
> reason: @"Flump cannot be initialized
> with -init"
> userInfo: nil];
> }
>
> But this didn’t fix it. The new -init method gets an error “Convenience
> initializer missing a ‘self’ call to another initializer”. The only way
> to get rid of this error is to add a call to -initWithString … but
> there’s no valid parameter I can pass because the caller didn’t provide a
> string. Nor can I put this -init call after the throw, because then the
> compiler complains that it’s unreachable.
>
> It seems as though whoever designed this hadn’t thought through the case
> where a subclass wants to _get rid of_ a superclass’s designated
> initializer, i.e. make it illegal to call it.
You should be able to use the NS_UNAVAILABLE macro in your @interface to
"delete" -init from your subclass, and thus suppress the warning.
(You'll need to keep your asserting implementation of -init in case
someone manages to send that message regardless.)
--Kyle Sluder
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden