user8065913
user8065913

Reputation:

Class has No Initializers Error Realm Class Swift

I'm getting a Class has no initializers Error. Is this not how I would set up the class. This is for a Realm OBject Database, the Properties I want to store. I'm new to swift and realm, but I figured this is how it would be done. If I take out the init() function and just assign everything values it works, however properties like justTook and all that I don't want start with a value, I need to set it later in the program.

Here is my code:

class MedInfo: Object {
    //  Info To Be Stored
    @objc dynamic var keyID = UUID().uuidString
    @objc dynamic var medName: String
    @objc dynamic var medDose: String
    @objc dynamic var currentDate: Date
    @objc dynamic var timeTook: Date
    @objc dynamic var lastTook: Date
    @objc dynamic var durationOfTime: Date
    @objc dynamic var doctorName: String
    @objc dynamic var rxDate: Date
    @objc dynamic var medInfo: String
    @objc dynamic var pictureOfMed: UIImage

    override static func primaryKey() -> String? {
        return "keyID"
    }

    convenience init(medName: String, medDose: String, currentDate: Date, timeTook: Date, lastTook: Date, durationOfTime: Date, doctorName: String,rxDate: Date, medInfo: String, pictureOfMed: UIImage) {
        self.medName = medName
        self.medDose = medDose
        self.currentDate = currentDate
        self.timeTook = timeTook
        self.lastTook = lastTook
        self.durationOfTime = durationOfTime
        self.doctorName = doctorName
        self.rxDate = rxDate
        self.medInfo = medInfo
        self.pictureOfMed = pictureOfMed
    }

Upvotes: 1

Views: 470

Answers (3)

lajosdeme
lajosdeme

Reputation: 2407

As @ces already correctly quoted from the Realm documentation: "This means that all non-optional properties must have a default value."

This is why I think the best way for you to create your Realm object is to declare your variables as optionals.

This way you will not have to call init. This is the way I create my own Realm objects and the way I've seen most people create them.

So your object would look like this:

class MedInfo: Object {
//  Info To Be Stored
@objc dynamic var keyID = UUID().uuidString
@objc dynamic var medName: String?
@objc dynamic var medDose: String?
@objc dynamic var currentDate: Date?
@objc dynamic var timeTook: Date?
@objc dynamic var lastTook: Date?
@objc dynamic var durationOfTime: Date?
@objc dynamic var doctorName: String?
@objc dynamic var rxDate: Date?
@objc dynamic var medInfo: String?
@objc dynamic var pictureOfMed: UIImage?

override static func primaryKey() -> String? {
    return "keyID"
}

Upvotes: 0

Chris Shaw
Chris Shaw

Reputation: 1610

You need to set default values for all parameters, as vaguely described in the docs. From here:

When using Realm from Swift, the Swift.reflect(_:) function is used to determine information about your models, which requires that calling init() succeed. This means that all non-optional properties must have a default value.

All the examples show this, so follow them. Your class should thus be declared:

class MedInfo: Object {
    //  Info To Be Stored
    @objc dynamic var keyID = UUID().uuidString
    @objc dynamic var medName: String = ""
    @objc dynamic var medDose: String = ""
    @objc dynamic var currentDate: Date = Date()
    @objc dynamic var timeTook: Date = Date()
    @objc dynamic var lastTook: Date = Date()
    @objc dynamic var durationOfTime: Date = Date()
    @objc dynamic var doctorName: String = ""
    @objc dynamic var rxDate: Date = Date()
    @objc dynamic var medInfo: String = ""

    // UIImage not supported, you'll need to store a URL/filename or something else
    @objc dynamic var pictureOfMed: UIImage

Upvotes: 2

andercruzbr
andercruzbr

Reputation: 454

You must call the init to create the instance.

convenience init(medName: String, medDose: String, currentDate: Date, timeTook: Date, lastTook: Date, durationOfTime: Date, doctorName: String,rxDate: Date, medInfo: String, pictureOfMed: UIImage) {

    self.init() 

    self.medName = medName
    self.medDose = medDose
    self.currentDate = currentDate
    self.timeTook = timeTook
    self.lastTook = lastTook
    self.durationOfTime = durationOfTime
    self.doctorName = doctorName
    self.rxDate = rxDate
    self.medInfo = medInfo
    self.pictureOfMed = pictureOfMed
}

Upvotes: 0

Related Questions