import RealmSwift
import Realm
public class Card : Object {
dynamic var username: String = ""
dynamic var firstName: String = ""
dynamic var lastName: String = ""
convenience init?(dictionary: [String:Any]?) {
guard let dictionary = dictionary , let username = dictionary["username"] as? String else { return else}
self.init()
self.username = username
self.firstName = firstName
self.lastName = lastName
}
required public init() {
fatalError("init() has not been implemented")
}
required public init( realm: RLMRealm, schema: RLMObjectSchema) {
fatalError("init(realm:schema:) has not been implemented")
}
required public init( value: Any, schema: RLMSchema) {
fatalError("init(value:schema:) has not been implemented")
}
}
As per suggestions I made the variables dynamic var as opposed to var and initialized them to empty strings. Initially I had the convenience init() as just init(). After adding realm the convenience init() calls self.init() as per suggestions. Now the default implementation asks throws
(fatalError("init() has not been implemented")
What should be inside the required public init()? Do I have to initialize the variables again?
As I mentioned in my answer to your previous question, by switching your init?
method to a convenience initializer, it's no longer necessary to override the various required initializers from the superclass. You can simply remove the three required public init
methods from your subclass.
public class Card : Object {
dynamic var username: String = ""
dynamic var firstName: String = ""
dynamic var lastName: String = ""
convenience init?(dictionary: [String:Any]?) {
guard let dictionary = dictionary,
let username = dictionary["username"] as? String,
let firstName = dictionary["firstName"] as? String,
let lastName = dictionary["lastName"] as? String
else { return nil }
self.init()
self.username = username
self.firstName = firstName
self.lastName = lastName
}
}