gpt4 book ai didi

ios - 找不到父类(super class) '-init' 的指定初始值设定项的 Objective-C 警告方法重写

转载 作者:技术小花猫 更新时间:2023-10-29 10:32:51 26 4
gpt4 key购买 nike

我正在清理应用程序中的警告,我两次收到此警告

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

对于这行代码

@implementation AFNetworkReachabilityManager

还有这一行

@implementation AFURLConnectionOperation

我是 objective-c 的新手,用 google 搜索了这个警告,只是不明白解决方案

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

最佳答案

来自 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 - 找不到父类(super class) '-init' 的指定初始值设定项的 Objective-C 警告方法重写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32741123/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com