Xcode 7.1和Swift 2.1以及最新的Realm Swift 0.96.2
我为Realm创建了一个模型类,但它不断地抛出有关inits的错误。我理解了子类的一定程度的初始化程序,但我无法解决这个问题以及它失败的原因。这是我上课的时间:
import UIKit
import RealmSwift
class Boxes: Object {
dynamic var precessor: String = "B";
dynamic var id: Int = 0;
dynamic var boxNumber: String {
return "\(precessor) \(id)"; //computed property
}
dynamic var boxDescription: String? = "";
dynamic var brand: String? = "";
dynamic let dateCreated: NSDate
dynamic var dateUpdated: NSDate?
dynamic var photo: UIImage?
dynamic var tags: NSArray? = [];
override static func primaryKey() -> String? {
return "id"; //sets primary key of the model
}
init(precessor: String, id: Int, description: String, brand: String, dateCreated: NSDate, dateUpdated: NSDate) {
self.precessor = precessor;
self.boxDescription = description;
self.brand = brand;
self.dateUpdated = dateUpdated;
self.dateCreated = dateCreated;
super.init();
}
}
当我尝试时,这不会构建,它告诉我:
'required' initializer 'init()' must be provided by subclass of 'Object'
我需要添加这一行:
required init() {
fatalError("init() has not been implemented")
}
让编译器满足于让我构建项目。但是当我运行项目时,它总是出错并在输出中给出fatalError行。我知道这是最后的手段,但我无法弄清楚原因。
这与我在某个地方失踪的超级初始化程序有关吗?我对swift相对较新,但如果我不用Object
答案 0 :(得分:6)
您需要实施init()
,但Xcode并不知道如何为您实施,因此它会放入fatalError("init() has not been implemented")
以提醒您实施它。
你可能只想打电话给超级。所以:
required init() {
super.init()
}