我在 swift 2 中构建我的应用程序时遇到问题。Xcode 说:
'required' initializer 'init(coder:)' must be provided by subclass of 'UIView'
这是类的代码:
class creerQuestionnaire: UIView {
@IBOutlet weak var nomQuestionnaire: UITextField!
@IBOutlet weak var question: UITextField!
@IBOutlet weak var reponse: UITextField!
var QR: Questionnaire
@IBAction func creerQuestion(sender: AnyObject) {
QR.ajouterQuestion(question.text!, nouvReponse: reponse.text!)
}
}
这是类(class)问卷:
import Foundation
class Questionnaire {
var QR = [String(), String()]
func getQuestion(nbQuestion: Int) ->String {
return QR[nbQuestion]
}
func getReponse(nbReponse: Int) ->String {
return QR[nbReponse]
}
func ajouterQuestion(nouvQuestion: String, nouvReponse: String) {
QR += [nouvQuestion, nouvReponse]
}
}
谢谢!
最佳答案
对于 required 的注意事项:在类初始值设定项的定义之前写入 required 修饰符,以指示该类的每个子类都必须实现该初始值设定项。
override 注意事项:在重写父类(super class)的指定初始化器时,您总是要写 override 修饰符,即使您的子类的初始化器实现是便利初始化器。
以上两个注释均来自:Swift Programming Language/Initialization
因此,您的 UIView 子类应该类似于下面的示例:
class MyView: UIView {
...
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
...
}
关于ios - swift : Error: 'required' initializer 'init(coder:)' must be provided by subclass of 'UIView' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35255466/