Gene Tucciarone
Gene Tucciarone

Reputation: 1

Swift: Realm error at init "NULL is not supported as an RLMObject property". But I don't have a NULL

It seems most people with this error are trying to create null strings. I just have three properties

dynamic var babyEvent: Int
dynamic var eventDate: NSDate
dynamic var timeSpent: Int

which are initialized in init() to

override init()
{
    self.babyEvent = BabyWet
    self.eventDate = NSDate()
    self.timeSpent = 5
    super.init()
}

but by the time super.init() is called I get '(null)' is not supported as an RLMObject property.

There are two Ints and one NSDate, all of which are valid Realm property types. So why am I getting this error?

Upvotes: 0

Views: 751

Answers (3)

jpsim
jpsim

Reputation: 14409

Realm doesn't support Swift enum's with no raw value. But adding a raw type to the BabyEvent enum and assigning the raw value to your realm objects works:

enum BabyEvent: Int {
  case BabyWet, case BabyDry
}
class MyRealmObject: RLMObject {
  dynamic var babyEvent = BabyEvent.BabyWet.rawValue
  dynamic var eventDate = NSDate()
  dynamic var timeSpent = 0
}

Upvotes: 1

Gusutafu
Gusutafu

Reputation: 755

As I wrote, you can just set the starting values in the model definitions:

class TestClass: RLMObject {
    dynamic var babyEvent: Int = 1
    dynamic var eventDate: NSDate = NSDate()
    dynamic var timeSpent: Int = 5    
}

but this also works for me:

class TestClass: RLMObject {
    dynamic var babyEvent: Int
    dynamic var eventDate: NSDate
    dynamic var timeSpent: Int

    override init() {
        babyEvent = 1
        eventDate = NSDate()
        timeSpent = 5
        super.init()
    }
}

In both cases I simply use

let realm = RLMRealm.defaultRealm()

var myTestObject = TestClass()

realm.beginWriteTransaction()
realm.addObject(myTestObject)
realm.commitWriteTransaction()

to create and add the object to the Realm.

Upvotes: 0

Anorak
Anorak

Reputation: 3116

In Swift Enums have a specific type. So while you think you are passing an Int for BabyWet you are actually passing something of that specific type.

It seems that you have an Enum for the BabyEvent, so you should really have a look at the rawValue property:

self.babyEvent = BabyWet.rawValue

Upvotes: 0

Related Questions