将通用元素附加到Realm List

时间:2018-01-15 15:13:27

标签: swift realm realm-list

我正在尝试通过对象架构属性在swift中设置Realm数据库中的数据。添加新对象很简单(并且还与另一个对象相关联,但是我无法找到将对象附加到List属性的方法。

让我们看一下我用来创建对象并添加到Realm数据库的代码片段:

func save(elements: [String : Any], realmClassName: String, realm: Realm, listObject: Object) {
    // Ge properties for Realm Object subclass with name realmClassName
    let properties = realm.schema[realmClassName]

    // Iterate through elements array
    for element in elements {
        // Instantiate Realm Object through its name (realmClassName)
        let thisClass: AnyClass = NSClassFromString("\(namespace).\(name)")!
        let realmClass = thisClass as! Object.Type
        let object = realmClass.init()

        // Iterate though Object Schema properties
        for property in properties?.properties as [Property]! {
           object[property.name] = element[property.name]
        }

        // Add Object to Realm database
        realm.add(object)

       // Here is where I want to append object to Object List
    }
 }

问题是,如何做类似的事情:

listObject.append(object)

有些尝试投掷错误,如:

Could not cast value of type 'RealmSwift.List<AppName.Cars>' (0x1117446d8) to 'RealmSwift.List<RealmSwift.Object>'

感谢。

0 个答案:

没有答案