2016-08-25 49 views
1

我想創建一個由不同類類型組成的通用領域列表(但是所有超類Object)。從領域中的通用列表獲取反向關係

class Parent: Object {  
    var children = List<Object>() 
} 

class Child1: Object { 
let parents = LinkingObjects(fromType: Parent.self, property: "children") 
} 

class Child2: Object { 
let parents = LinkingObjects(fromType: Parent.self, property: "children") 
} 

Child1Child2實例爲Parent對象工作正常,但是,逆關係提供了以下錯誤:

- Property ‘Parent.children’ declared as origin of linking objects property ‘Child1.parents’ links to a different class. 
- Target type 'RealmSwiftObject' doesn't exist for property ‘children’. 

怎麼可能在parents屬性正確地確定它保證?我可以想象它應該根據它所屬的類別類型進行過濾(即Child1Child2),但我不知道該怎麼做?

回答

0

除非有我不知道的解決方法,否則不可能在List中存儲不同的類型。

List s包含單一類型的其他Object s並且具有與可變的Array非常相似的接口。 (https://realm.io/docs/swift/latest/#to-many-relationships

+0

這是正確的。 Realm通常不支持類型多態。有關詳細信息和解決方法,請參閱https://realm.io/docs/swift/latest/#model-inheritance。 – jpsim

+0

我在文檔中忽略了這一點,但我會盡力找到解決方法。 – Taco