[英]Initializer does not override a designated initializer from its superclass, Swift 2.0
Working thru this appcoder tutorial building a text to speech app tutorial and run into a swift 1.2-> swift 2.0 feature. 通过此Appcoder教程进行工作, 构建了文本到语音的应用程序教程,并遇到了Swift 1.2-> Swift 2.0功能。
The demo project has a class in it... which the Xcode 7.1.1 under IOS 10.11.1 doesn't like, giving me the error. 演示项目中有一个类... IOS 10.11.1下的Xcode 7.1.1不喜欢,给我错误。 "initialiser doesn't override the designated initialiser from its super class" on the override init() statement, followed by the error "Must call the designated initialiser of the superclass UISlider".
覆盖init()语句上的“初始化器不会覆盖其父类中的指定初始化器”,然后出现错误“必须调用超类UISlider的指定初始化器”。
Ok, Rather new to Swift; 好的,对Swift来说不是新手。 and quite where I start to fix this is a little lost on me.
在我开始解决此问题的地方,我有些不知所措。
class CustomSlider: UISlider {
var sliderIdentifier: Int!
override init() { <--- error here
super.init() <--- error here
sliderIdentifier = 0
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
sliderIdentifier = 0
}
}
What should this code snippet say? 该代码段应该怎么说?
Since UISlider
doesn't define or inherit init()
as a designated initializer, you can't call it from your subclass init
. 由于
UISlider
不会将init()
定义或继承为指定的初始化程序,因此您不能从子类init
调用它。
class CustomSlider: UISlider {
var sliderIdentifier: Int!
required init() {
super.init(frame: CGRect.zero)
sliderIdentifier = 0
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
sliderIdentifier = 0
}
}
In this case, you could factor out the initialization of sliderIdentifier
and just inherit all of UISlider
s initializers: 在这种情况下,您可以排除
sliderIdentifier
的初始化,而只继承所有UISlider
的初始化器:
class CustomSlider: UISlider {
var sliderIdentifier: Int! = 0
}
For more information, read the section titled “Designated Initializers and Convenience Initializers” in The Swift Programming Language . 有关更多信息,请阅读Swift编程语言中标题为“指定的初始化程序和便捷初始化程序”的部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.