1

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?

  • 2
    I think that's just a typo in the documentation, `enum class` doesn't make sense, it should just be `@objc enum MyEnum: Int, RealmEnum {` – dan Apr 02 '20 at 15:46

1 Answers1

3

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>() 
}