我在以下代码上遇到调试错误:
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
以下是我的所有代码:
class ViewController: UIViewController {
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
var button = UIButton.buttonWithType(.Custom) as UIButton
button.frame = CGRectMake(160, 100, 50, 50)
button.layer.cornerRadius = 0.5 * button.bounds.size.width
button.setImage(UIImage(named:"thumbsUp.png"), forState: .Normal)
button.addTarget(self, action: "thumbsUpButtonPressed", forControlEvents: .TouchUpInside)
view.addSubview(button)
}
func thumbsUpButtonPressed() {
println("thumbs up button pressed")
}
错误说:
Thread 1: EXC_BAD_INSTRUCTION (code=EXC_1386_INVOP, subcode=0x0)
调试器输出如下所示:
fatal error: init(coder:) has not been implemented: file...hi/ViewController.swift, line 14
之前我遇到过这个错误,我不确定如何修复它。奇怪的是,有一个错误说You don't have any initializers
让我把它放进去。它给了我一个错误(无论有没有)。
非常感谢任何意见和建议。
提前致谢。
答案 0 :(得分:1)
在您的代码中,您触发“fatalError”而不是初始化。取代
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
与
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}