swift – 只能从它所属的Realm中删除一个对象

swift – 只能从它所属的Realm中删除一个对象,第1张

概述我每次尝试从我的tableview上的域中删除一个对象时,我都会收到此错误“只能从它所属的域中删除一个对象”.这是相关代码: let realm = try! Realm()var checklists = [ChecklistDataModel]()override func viewWillAppear(_ animated: Bool) { checklists = [] 我每次尝试从我的tablevIEw上的域中删除一个对象时,我都会收到此错误“只能从它所属的域中删除一个对象”.这是相关代码:
let realm = try! Realm()var checkLists = [CheckListDataModel]()overrIDe func vIEwWillAppear(_ animated: Bool) {    checkLists = []    let getCheckLists = realm.objects(CheckListDataModel.self)    for item in getCheckLists{        let newCheckList = CheckListDataModel()        newCheckList.name = item.name        newCheckList.note = item.note        checkLists.append(newCheckList)    }    tableVIEw.reloadData()}overrIDe func numberOfSections(in tableVIEw: UItableVIEw) -> Int {    return 1}overrIDe func tableVIEw(_ tableVIEw: UItableVIEw,numberOfRowsInSection section: Int) -> Int {    return checkLists.count}overrIDe func tableVIEw(_ tableVIEw: UItableVIEw,cellForRowAt indexPath: IndexPath) -> UItableVIEwCell {    let cell = tableVIEw.dequeueReusableCell(withIDentifIEr: "CheckListCell",for: indexPath) as! ListstableVIEwCell    cell.name.text = checkLists[indexPath.row].name    return cell}overrIDe func tableVIEw(_ tableVIEw: UItableVIEw,commit editingStyle: UItableVIEwCellEditingStyle,forRowAt indexPath: IndexPath) {    if editingStyle == .delete {        // Delete the row from the data source        try! realm.write {            realm.delete(checkLists[indexPath.row])        }        //delete locally        checkLists.remove(at: indexPath.row)        self.tableVIEw.deleteRows(at: [indexPath],with: .fade)    }}

我知道这部分是具体的:

// Delete the row from the data source        try! realm.write {            realm.delete(checkLists[indexPath.row])        }

对于发生了什么的任何想法?
提前致谢!

您正在尝试删除存储在集合中的Realm对象的副本,而不是存储在Realm中的实际Realm对象.
try! realm.write {    realm.delete(Realm.objects(CheckListDataModel.self).filter("name=%@",checkLists[indexPath.row].name))}

如果没有ChekListDataModel的定义,我不确定我是否正确使用nspredicate,但你应该能够从这里弄明白.

总结

以上是内存溢出为你收集整理的swift – 只能从它所属的Realm中删除一个对象全部内容,希望文章能够帮你解决swift – 只能从它所属的Realm中删除一个对象所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1089992.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存