Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C warning Method override for the designated initializer of the superclass '-init' not found

I am cleaning up the warnings in an app and I got this warning twice

Method override for the designated initializer of the superclass '-init' not found

for this line of code

@implementation AFNetworkReachabilityManager

and this line

@implementation AFURLConnectionOperation

I am fairly new to objective-c and googled this warning and just didn't understand the solutions

My question is how do I get rid of these warnings?

like image 732
user979331 Avatar asked Sep 23 '15 13:09

user979331


1 Answers

From Apple forums

The rules for designated initialisers are complex and I'm going to bounce you to the docs for the general case. Curiously, I've found the best explanation of this to be the "Initialization" section of The Swift Programming Language, because the same concepts apply to both Swift and Objective-C. In your specific case you should override -init and have it fail at runtime. You should also tag it in your header with NS_UNAVAILABLE which will allow the compiler to catch this in the typical case. The above applies because your class can't possibly operate without a Model, and thus you can't reasonably implement -init in any useful way. If you could, you should. For example, if you were creating your own string object, it would make sense for it to implement -init by calling super and then initialising the string to the empty string.

In your .h files:

@interface MyClass : NSObject  
- (instancetype)init NS_UNAVAILABLE;  

@end  

In .m files:

@interface MyClass ()  
- (instancetype)init NS_DESIGNATED_INITIALIZER;  
@end  

@implementation MyClass  
 - (instancetype)init { @throw nil; }  

@end  
like image 96
BoilingLime Avatar answered Nov 09 '22 05:11

BoilingLime