【发布时间】:2020-04-02 14:17:16
【问题描述】:
所以我最近遇到了这个问题,我编写的一些 Swift 5 代码在 Xcode 11.0 但不是 11.2.1 中编译,后者抱怨我的扩展 UIViewController 的类没有默认初始化程序(它定义了 no初始化程序)当我尝试实例化它时。
确实,当我查看 UIViewController 的定义时,只有以下两个定义:
public init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?)
public init?(coder: NSCoder)
它还包含一条注释,似乎暗示也应该存在一个默认初始化程序,但我找不到它。
/*
The designated initializer. If you subclass UIViewController, you must call the super implementation of this
method, even if you aren't using a NIB. (As a convenience, the default init method will do this for you,
and specify nil for both of this methods arguments.) In the specified NIB, the File's Owner proxy should
have its class set to your view controller subclass, with the view outlet connected to the main view. If you
invoke this method with a nil nib name, then this class' -loadView method will attempt to load a NIB whose
name is the same as your view controller's class. If no such NIB in fact exists then you must either call
-setView: before -view is invoked, or override the -loadView method to set up your views programatically.
*/
我找到了一种解决方法来完成这项工作,我只需要定义调用超类初始化程序的所有三个初始化程序,默认工作如评论中所述,但我仍然困惑我是如何逃脱的当我在任何地方都找不到它时,一直使用默认初始化程序。注释中描述的默认初始化器实际定义在哪里?
【问题讨论】:
-
显示您的代码。