0
votes
enum DummyEnum {
    case spiderMan
    case anotherMan
    case tanos
    case duppy

    var associatedClass: Object.Type {
        switch self {
        case .spiderMan:
            return WebEntity.self
        default:
            return IronManEntity.self
        }
    }
}

realm.objects(DummyEnum().spiderMan.associatedClass) // returns crash
realm.objects(WebEntity.self) // done well

Terminating app due to uncaught exception 'RLMException', reason: 'Object type 'RealmSwiftObject' is not managed by the Realm. If using a custom objectClasses / objectTypes array in your configuration, add RealmSwiftObject to the list of objectClasses / objectTypes.'

Why the crash is happened and how to resolve it? I need a var for enum to associate Realm objects with enum cases.

1

1 Answers

0
votes

The code works for me with one change - I used a DogClass and PersonClass for testing so I have a class enum like yours

enum realmClasses {
    case dog
    case person

    var associatedClass: Object.Type {
        switch self {
        case .dog:
            return DogClass.self
        case .person:
            return PersonClass.self
        }
    }
}

and then inside a function to read all of the dogs

let realm = try! Realm()
let dogResults = realm.objects(realmClasses.dog.associatedClass)
for dog in dogResults {
    print(dog)
}

The difference is in how the enum is accessed. You have this

DummyEnum().spiderMan.associatedClass
         ^^

and I have this, noting no () as that's not needed for an enum for my implementation

realmClasses.dog.associatedClass

Oh - any my DogClass and PersonClass look like this

class DogClass: Object {
    @objc dynamic var dog_id = NSUUID().uuidString
    @objc dynamic var dog_name = ""

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