当我在 swift 2 中构建我的应用程序时遇到问题。Xcode 说:
“required”初始化程序“init(coder:)”必须由“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!)
}
}
这是班级调查问卷:
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]
}
}
Merci!
注意事项required:在类初始值设定项的定义之前写入所需的修饰符,以指示该类的每个子类都必须实现该初始值设定项。
注意事项override:当重写超类指定的初始化器时,您总是编写 override 修饰符,即使您的子类的初始化器实现是便利初始化器。
以上两个注释均引用自:Swift 编程语言/初始化 https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Initialization.html
因此,您的 UIView 子类应类似于以下示例:
class MyView: UIView {
...
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
...
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)