Xcode 7.1 and Swift 2.1 with latest Realm Swift 0.96.2
I created a model class for Realm but it is constantly throwing errors about inits. I understand initializers to an extent for subclasses but I can't wrap my head around this and why it fails. Here's the class I made:
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();
}
}
This won't build when I try and it tells me:
'required' initializer 'init()' must be provided by subclass of 'Object'
And that I need this line added:
required init() {
fatalError("init() has not been implemented")
}
That satiates the compiler enough to let me build the project. However when I run the project, it always errors out and give me the fatalError line in the output. I know it's doing this as a last resort initializer but I can't figure out why.
Is this related to a super initializer I'm missing somewhere? I'm relatively new to swift but I can get my initializers to work if I don't subclass my class with Object
You are required to implement init() but Xcode doesn't know how to implement if for you so it puts in fatalError("init() has not been implemented") to remind you to implement it.
You probably just want to call super. So:
required init() {
super.init()
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With