ios – Objective-C警告未找到超类“-init”的指定的初始化程序的方法覆盖

前端之家收集整理的这篇文章主要介绍了ios – Objective-C警告未找到超类“-init”的指定的初始化程序的方法覆盖前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在一个应用程序中清理警告,我收到了两次这个警告
Method override for the designated initializer of the superclass '-init' not found

对于这行代码

@implementation AFNetworkReachabilityManager

和这一行

@implementation AFURLConnectionOperation

我相当新的Objective-C和谷歌这个警告,只是不明白的解决方

我的问题是如何摆脱这些警告?

解决方法

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.

在.h文件中:

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

@end

在.m文件中:

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

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

@end

猜你在找的iOS相关文章