gpt4 book ai didi

ios - UIViewController 的默认初始化在哪里?

转载 作者:行者123 更新时间:2023-12-01 16:21:18 24 4
gpt4 key购买 nike

所以我最近遇到了这个问题,我编写的一些 Swift 5 代码在 Xcode 11.0 但不是 11.2.1 中编译,后者提示我的类没有默认初始化程序扩展 UIViewController(它没有定义初始化程序)时我尝试实例化它。

事实上,当我查看 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.
*/

我找到了一种解决方法来完成这项工作,我只需要定义调用父类(super class)初始化程序的所有三个初始化程序,默认工作方式如评论中所述,但我仍然对使用当我在任何地方都找不到它时,一直都是默认初始化程序。注释中描述的默认初始化程序实际定义在哪里?

最佳答案

通常,当您定义一个变量而不初始化它时,就会发生这种情况。因此,如果您的代码中有这样的变量,例如-

var name:string

改变它
var name:string = "" 

或者
var name = string()

关于ios - UIViewController 的默认初始化在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59249535/

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