Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add a subclass to a List<superclass> in Realm Swift

I have a superclass called Thing (I know it's not specific) that inherits from Object:

class Thing: Object {
    dynamic var title: String = ""

    var parents: [Thing] {
        return linkingObjects(Thing.self, forProperty: "children")
    }

    let children = List<Thing>()
}

and two subclasses of that called Task and Thought.

However, when I do

someThing.children.append(Task())

it throws "Object type is incorrect."

What am I doing wrong? Does realm not allow subclasses in lists of superclasses because that doesn't make much sense.

like image 885
teo751 Avatar asked Sep 14 '15 05:09

teo751


1 Answers

Currently, Realm's inheritance mapping doesn't support yet polymorphism. You would need either dedicated lists with a concrete subclass as generic parameter or a whole different approach of inheritance mapping.

like image 181
marius Avatar answered Nov 03 '22 17:11

marius