如何在Realm Swift中级联删除具有属性List <int>的对象

时间:2019-01-28 18:27:49

标签: swift list int realm

我在List<Int>中具有类型RealmObject的属性。但是我无法执行级联删除操作的原因。

我拥有属性List<Int>(),然后在init中将这个属性数组设置为一些整数。然后我试图删除对象并出现下一个错误:

  

无法将类型'__NSCFNumber'(0x10dba87d8)的值强制转换为'RLMObjectBase'(0x10b21b708)。

     

2019-01-27 01:10:43.524057 + 0300 Ibiza [25129:3982818]无法将类型'__NSCFNumber'(0x10dba87d8)的值强制转换为'RLMObjectBase'(0x10b21b708)。

这是我的层叠删除函数实现:

func cascadeDelete(_ entity: RLMObjectBase) {
    guard let entity = entity as? Object else { return }
    var toBeDeleted = Set<RLMObjectBase>()
    toBeDeleted.insert(entity)
    while !toBeDeleted.isEmpty {
        guard let element = toBeDeleted.removeFirst() as? Object,
            !element.isInvalidated else { continue }
        resolve(element: element, toBeDeleted: &toBeDeleted)
    }
}

func resolve(element: Object, toBeDeleted: inout Set<RLMObjectBase>) {
    element.objectSchema.properties.forEach {
        guard let value = element.value(forKey: $0.name) else { return }
        if let entity = value as? RLMObjectBase {
            toBeDeleted.insert(entity)
        } else if let list = value as? RealmSwift.ListBase {
            for index in 0..<list._rlmArray.count {
                toBeDeleted.insert(list._rlmArray.object(at: index) as! RLMObjectBase)                 }
        }
    }
    delete(element)
}

此代码行中的错误:

toBeDeleted.insert(list._rlmArray.object(at: index) as! RLMObjectBase)

0 个答案:

没有答案