需要初始化一个对象,但我的初始化器不起作用。到目前为止我已经知道了,但我不确定如何将参数传递给对象初始值设定项。
init(coder aDecoder: NSCoder, caseImage: UIImage, inputName: String, inputDate: String) {
self.caseImage = caseImage
self.caseName.text = inputName
self.caseDate.text = inputDate
super.init( coder: aDecoder )
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
此代码正在另一个类中调用初始值设定项:
func loadSampleData() {
let Photo1 = UIImage(named: "retino1")!
let case1 = TableCell(caseImage: Photo1, inputName: "John Smith", inputDate: "2015-10-18" ) //error
patientCaseArray = [case1]
}
最佳答案
根据您发布的内容,您必须在init语句中传入解码器。下面是您要调用的内容,它无效,因为它不存在:
TableCell(UIImage, String, String )
所以你要么创建它:
init(caseImage: UIImage, inputName: String, inputDate: String) {
super.init( coder: NSCoder() )
self.caseImage = caseImage
self.caseName.text = inputName
self.caseDate.text = inputDate
}
或者传入NSCoder():
let case1 = TableCell(coder:NSCoder(), caseImage: Photo1, inputName: "John Smith", inputDate: "2015-10-18" )