ios Swift, super.init() Must call a specified initializer of the superclass 'UIView' 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30887339/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 06:28:24  来源:igfitidea点击:

Swift, super.init() Must call a designated initializer of the superclass 'UIView' error

iosiphoneswift

提问by EberronBruce

I am new with Swift. I inherited a project. I saw it running on a device. However, when I checkout the code and it had many errors. I was able to clear out the errors. However, I run into this one that is baffling me. The project uses xib files as well. Here is the code.

我是 Swift 的新手。我继承了一个项目。我看到它在设备上运行。但是,当我检查代码时,它有很多错误。我能够清除错误。然而,我遇到了这个让我莫名其妙的问题。该项目也使用 xib 文件。这是代码。

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
    }

   init(items:NSArray, viewController:AnyObject){
        super.init()
        //itemsArray = items
        itemsArray = items as [AnyObject]
        //commonInit(viewController as UIViewController)
        commonInit(viewController as! UIViewController)
    }

I get the error under the init(items:NSArray, viewController:AnyObject) method/function. The error is pointed at the "super.init()". It states "Must call a designated initializer of the superclass 'UIView' error"

我在 init(items:NSArray, viewController:AnyObject) 方法/函数下收到错误。错误指向“super.init()”。它指出“必须调用超类‘UIView’错误的指定初始值设定项”

I been searching, googling, asking others and nothing has turned up. Can I get help on fixing this error, or at least why this error happens? I would like understand so I can become a better software developer.

我一直在搜索,谷歌搜索,询问其他人,但没有任何结果。我能否获得修复此错误的帮助,或者至少为什么会发生此错误?我想了解,以便我可以成为更好的软件开发人员。

Edit: I would like to thank everyone for their insight and help. I found out the problem is bigger. I did the changes suggested in the super.init(frame: CGRect). I had to change an array property as well which was affecting the init function.

编辑:我要感谢大家的洞察力和帮助。我发现问题更大了。我做了 super.init(frame: CGRect) 中建议的更改。我还必须更改影响 init 函数的数组属性。

回答by The Tom

As the error message suggests you can only call the designated initializer of the superclass.

由于错误消息表明您只能调用超类的指定初始化程序。

To solve this you need to call : super.init(frame: frame)instead of super.init()

要解决这个问题,您需要调用 :super.init(frame: frame)而不是super.init()

回答by Tomas Camin

UIView's designated initializers are:

UIView的指定初始值设定项是:

init(frame: CGRect)
init(coder aDecoder: NSCoder)

You're calling super.init()which is an initializer inherited from its superclass NSObject. Use one of those initialisers instead as the compiler is suggesting.

您正在调用super.init()which 是从其超类继承的初始值设定项NSObject。按照编译器的建议使用这些初始化程序之一。