List of multiple object types?


#1

Hello,

Is it possible to create lists with multiple object types? For example:

class Animal: Object {
      let animalList = List<Object>() // Generic object type
}

class Dog: Object {
    var name: String = ""
    var parentList = LinkingObjects(fromType: Animal.self, property: "animalList")
}

class Cat: Object {
    var name: String = ""
    var parentList = LinkingObjects(fromType: Animal.self, property: "animalList")
}

Thank you.


#2

Your example looks like you’re dealing with some kind of inheritance. Realm does not fully support it but the accepted answere here, also posted in this forum should help you. In case inheritance is not your primary intention, you could still solve it with the pattern mentioned in the post by introducing a “GenericObject” superclass or so that allows multiple subclasses.


#3

Thank you, Chuck! That was very helpful.