Tôi quyết định tiếp tục dự án còn lại của mình với Swift. Khi tôi thêm lớp tùy chỉnh (lớp con của UIViewcontroller) vào trình điều khiển chế độ xem bảng phân cảnh của mình và tải dự án, ứng dụng sẽ gặp sự cố bất ngờ với lỗi sau:
lỗi nghiêm trọng: sử dụng trình khởi tạo chưa được thực hiện 'init (coder :)' cho lớp
Đây là một mã:
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.
}
*/
}
Xin đề nghị một cái gì đó
init(style: UITableViewStyle) { super.init(style: style) // Custom initialization }Tại sao chúng ta có thể có hai hàm init? Và có ai biết tại sao Apple không bao gồm init thứ 2 theo mặc định không?