Search code examples
swiftrealm

Instantiating Realm model class crashes with missing key error


When trying to instantiate my RealmDouble model I get the following error:

*** Terminating app due to uncaught exception 'RLMException', reason: 'Invalid value '0' to initialize object of type 'RealmDouble': missing key 'double_value'

class RealmDouble: Object {
    dynamic var double_value: Double = 0.00
}

RealmDouble(value: 0.0)

I have tried deleting the app from the simulator, as well as deleting the Realm file. Does anyone know how to fix this?


Solution

  • You'd see this exception if you're passing a Double in a place where Realm expects to see either a dictionary from property names to values or an array of values, such as Realm.create(_:value:update). For instance, you'll see an error like this from the following code:

    realm.create(RealmDouble.self, value: 0.0)
    

    You should instead do:

    realm.create(RealmDouble.self, value: [0.0])