xcode Swift:属性未在 super.init 调用中初始化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25335239/
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
Swift: Property not initialized at super.init call
提问by NJanf
I am making a little custom keyboard and I have a set of variables inside the class that need to be initialized inside the 'override init()' as can be seen below:
我正在制作一个小的自定义键盘,我在类中有一组变量需要在“覆盖 init()”中初始化,如下所示:
class KeyboardViewController: UIInputViewController {
var button01: CharacterButton
var button02: CharacterButton
...
and
和
override init() {
//Initialize buttons and assign attributes
button01 = CharacterButton.createButton("Q", labelOfButton: "Q")
button02 = CharacterButton.createButton("W?", labelOfButton: "W")
super.init()
}
In addition to that I have also added the following code, as it is apparently required since Xcode 6 beta 5:
除此之外,我还添加了以下代码,因为 Xcode 6 beta 5 显然是必需的:
required init(coder aDecoder: NSCoder!) {
super.init(coder: aDecoder)
}
But that last snippet of code results in an error saying:
但是最后一段代码导致了一个错误:
Property 'self.button01' not initialized at super.init call
属性“self.button01”未在 super.init 调用中初始化
Why would this happen when I have already initialized 'button01' and called the 'super.init()' inside the former 'override init()' function? This is driving me nuts.
为什么当我已经初始化了 'button01' 并在之前的 'override init()' 函数中调用了 'super.init()' 时会发生这种情况?这让我发疯。
采纳答案by Ivica M.
init(coder:)
and init()
are two different designated initialisers. You have few options here. The best one would be to initialise your properties in-place.
init(coder:)
和init()
是两个不同的指定初始值设定项。您在这里几乎没有选择。最好的方法是就地初始化您的属性。
class KeyboardViewController: UIInputViewController {
var button01 = CharacterButton.createButton("Q", labelOfButton: "Q")
var button02 = CharacterButton.createButton("W?", labelOfButton: "W")
}
EDIT:
编辑:
Removed initializers from the above code. If all the properties are initialised in-place, there is no need for overriding any initializer. All of them will simply be inherited automatically (including init(coder:)
).
从上面的代码中删除了初始值设定项。如果所有属性都就地初始化,则无需覆盖任何初始化程序。所有这些都将简单地自动继承(包括init(coder:)
)。
Or you can do something like this, if your class is never going to be created from nibs (including storyboards:
或者你可以做这样的事情,如果你的班级永远不会从笔尖(包括故事板)创建:
class KeyboardViewController: UIInputViewController {
...
required init(coder aDecoder: NSCoder!) {
fatalError("This class doesn't support NSCoding.")
}
}
I don't like this solution, as it breaks the superclass' promise about implementing NSCoding, but it 'works'. Finally, you can make your properties be implicitly unwrapped optionals.
我不喜欢这个解决方案,因为它打破了超类关于实现 NSCoding 的承诺,但它“有效”。最后,您可以使您的属性成为隐式解包的选项。
class KeyboardViewController: UIInputViewController {
var button01: CharacterButton!
var button02: CharacterButton!
...
}
But if you do that, your init(coder:)
will become unsafe, so it's better to throw fatalError
from there and stick to non-optionals.
但是如果你这样做,你init(coder:)
会变得不安全,所以最好fatalError
从那里扔掉并坚持非可选。
回答by Luca Angeletti
This happens because if the class is created with the second initializer
发生这种情况是因为如果该类是使用第二个初始化程序创建的
init(coder aDecoder: NSCoder!)
then your variables button01 and button02 are never initialized. And since they are not optionals, the compiler does not allow this.
那么你的变量 button01 和 button02 永远不会被初始化。由于它们不是可选项,编译器不允许这样做。
You could change the second initializer to something like this:
您可以将第二个初始化程序更改为如下所示:
required init(coder aDecoder: NSCoder!) {
button01 = CharacterButton.createButton("Q", labelOfButton: "Q")
button02 = CharacterButton.createButton("W?", labelOfButton: "W")
super.init(coder: aDecoder)
}
Or you could initialize you variables when they are declared:
或者,您可以在声明变量时对其进行初始化:
var button01 = CharacterButton.createButton("Q", labelOfButton: "Q")
var button02 = CharacterButton.createButton("W?", labelOfButton: "W")
回答by Chris Wagner
If you're not interested in implementing init(coder:)
you should throw an error.
如果你对实现不感兴趣,init(coder:)
你应该抛出一个错误。
required init(coder aDecoder: NSCoder!) {
fatalError("use init() method")
}
I am not sure if the system invokes init(coder:)
for UIInputViewController
sub-classes however.
我不知道,如果系统将调用init(coder:)
的UIInputViewController
子类。然而。