简体   繁体   English

如何将子类添加到列表 <superclass> 在Realm Swift中

[英]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: 我有一个叫做Thing的超类(我知道它不是特定的),它继承自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. 领域是否不允许在超类列表中使用子类,因为这没有多大意义。

Currently, Realm's inheritance mapping doesn't support yet polymorphism. 当前,Realm的继承映射尚不支持多态。 You would need either dedicated lists with a concrete subclass as generic parameter or a whole different approach of inheritance mapping. 您可能需要带有一个具体子类作为通用参数的专用列表,或者需要一种完全不同的继承映射方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM