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

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...