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

teo751

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.

marius

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Pydantic: How do I call a superclass method inside a subclass validator?

Automatically add a subclass to a list when it extends a superclass

How to filter subclass objects from superclass Set and add them to subclass List

How do I subclass a struct in Swift?

How can do a Polymorphism with subclass in a superclass?

How do I use Realm in a Swift Playground?

If I have a collection of superclass type, how do I get all the items of a specific subclass type?

How can I define a function in a superclass that will use the subclass's type when subclassed in Swift?

Swift: How can I make a function with a Subclass return type conform to a protocol, where a Superclass is defined as a return type?

How do I pass ES6 default parameters from a subclass to its superclass?

log4j: How do I enable logging in a subclass for a method in a superclass

How do I use subclasses in a factory subclass to override abstract class in a superclass factory?

How do I add a superclass to two classes in Rails?

Overriden equals and hashcode methods of superclass. Do I do for subclass

If I import a superclass, do I need to import it's subclass too?

How to make superclass querysets return subclass objects? Or downcast the superclass queryset to a subclass list?

Swift: How do I add data to a variable of an object in a list of objects

How to add funtionallity to a Jbutton,located in superclass from a subclass?

How can I add or update a Realm object in Swift?

How to assertThat superclass list equal to subclass matcher using hamcrest and mockito

How can I filter a Realm List by Date Swift NSPredicates

How do I write a custom init for a UIView subclass in Swift?

How do I make a custom initializer for a UIViewController subclass in Swift?

In Swift, how do I prevent a function from being called on a subclass?

How can I to add object to the List<Foo> via Realm Browser

How do I subclass argparse.Action to add a custom action?

How do you call subclass method in superclass, in Java?

How do you call a subclass method from a superclass in Java?

How can I create instances of every subclass of a given superclass?