Swift:错误:'required'初始化程序'init(编码器:)'必须由'UIView'的子类提供 [英] Swift : Error: 'required' initializer 'init(coder:)' must be provided by subclass of 'UIView'
问题描述
我在swift 2中构建应用程序时遇到问题.Xcode说:
I have a problem when I build my app in swift 2. Xcode says:
'required'initializer'init(编码器: )'必须由'UIView'的子类提供
'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!)
}
}
这是课程调查问卷:
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!
推荐答案
注意必需:在定义类初始化程序之前写入必需的修饰符,以指示该类的每个子类都必须实现该初始化程序。
Note for required: Write the required modifier before the definition of a class initializer to indicate that every subclass of the class must implement that initializer.
注意覆盖:当覆盖超类指定的初始化程序时,总是编写覆盖修饰符,即使你的子类的初始化程序的实现是一个方便的初始化程序。
Note for override: You always write the override modifier when overriding a superclass designated initializer, even if your subclass’s implementation of the initializer is a convenience initializer.
以上两个注释均来自: Swift编程语言/初始化
Above both notes are referred from: Swift Programming Language/Initialization
因此,您的UIView子类应类似于以下示例:
Therefore, your subclass of UIView should look similar to the sample below:
class MyView: UIView {
...
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
...
}
这篇关于Swift:错误:'required'初始化程序'init(编码器:)'必须由'UIView'的子类提供的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!