teo751
teo751

Reputation: 321

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.

Upvotes: 6

Views: 949

Answers (1)

marius
marius

Reputation: 7806

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.

Upvotes: 4

Related Questions