ajackson55555
ajackson55555

Reputation: 41

How to use enums in RealmSwift now with the 4.0 update?

We used to be able to use enums in Realm Swift through getters and setters (see great solution here), but the latest update now requires us to conform to the RealmEnum protocol (link here). Being a beginner programmer, I unfortunately don't understand how to do this. Copying their code pops up a multitude of errors in XCode.

@objc enum class MyEnum: Int, RealmEnum { //says inheritance from non-protocol, non-class type 'Int'
    case thing1 = 1 //says enum case is not allowed outside of an enum
    case thing2 = 2
    case thing3 = 3
}

class MyModel: Object {
   @objc dynamic enumProperty = MyEnum.thing1 //says expected 'var' keyword in property declaration
   let optionalEnumProperty = RealmOptional<MyEnum>() //says 'MyEnum is ambiguous for type lookup
} 

How can I get enums working again in Realm Swift using the RealmEnum protocol?

Upvotes: 1

Views: 865

Answers (1)

ajackson55555
ajackson55555

Reputation: 41

Thanks Dan, I figured it out. Two typos in the Realm documentation (I'll contact them to try to get them to fix it.) Corrected code below:

@objc enum MyEnum: Int, RealmEnum { //deleted the word class
    case thing1 = 1 
    case thing2 = 2
    case thing3 = 3
}

class MyModel: Object {
   @objc dynamic var enumProperty = MyEnum.thing1 //added the word var
   let optionalEnumProperty = RealmOptional<MyEnum>() 
} 

Upvotes: 3

Related Questions