我决定继续与Swift一起完成剩余的项目。当我将自定义类(的子类UIViewcontroller
)添加到情节提要视图控制器并加载项目时,该应用突然崩溃,并出现以下错误:
致命错误:对类使用未实现的初始化程序“ init(coder :)”
这是一个代码:
import UIKit
class TestViewController: UIViewController {
init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
// Custom initialization
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// #pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue?, sender: AnyObject?) {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
}
请提出一些建议
init(style: UITableViewStyle) { super.init(style: style) // Custom initialization }
为什么我们有两个 init函数?以及为什么苹果默认不包含第二个init?