Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/99.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 删除域中一对多关系中的项_Ios_Swift_Realm_One To Many - Fatal编程技术网

Ios 删除域中一对多关系中的项

Ios 删除域中一对多关系中的项,ios,swift,realm,one-to-many,Ios,Swift,Realm,One To Many,以下代码适用于添加新的ItemLists和向ItemLists添加新的Itemss。我遇到的问题是从列表中删除所有项目。换句话说,我有一个按钮(deleteAllItems),它应该从所选列表中删除所有项目,但我现在使用它的方式,它会删除领域中的所有项目,而不管它们属于哪个列表 创建多个项目列表s的正确方法是什么?这些列表将包含多个项目s,并且能够从特定列表中删除所有项目s ItemList对象: 主控制器: 类ViewController:UIViewController,UITableVie

以下代码适用于添加新的
ItemList
s和向
ItemList
s添加新的
Items
s。我遇到的问题是从列表中删除所有项目。换句话说,我有一个按钮(
deleteAllItems
),它应该从所选列表中删除所有项目,但我现在使用它的方式,它会删除
领域中的所有
项目
,而不管它们属于哪个列表

创建多个
项目列表
s的正确方法是什么?这些列表将包含多个
项目
s,并且能够从特定列表中删除所有
项目
s

ItemList对象: 主控制器:
类ViewController:UIViewController,UITableViewDataSource{
@IBMOutlet弱var tableListOfItems:UITableView!
@iButProductName:UITextField!
@IBOutlet弱var inputListName:UITextField!
var allItems:结果!
重写func viewDidLoad(){
super.viewDidLoad()
更新数据()
}
@iAction func addNewList(u发件人:任意){
let list=ItemList()
list.listName=inputListName.text!
试试看!写吧{
realm.add(列表)
}
}
@iAction func addNewItem(\发送方:任意){
让newItem=Item()
newItem.productName=inputProductName.text!
让list=realm.objects(ItemList.self).filter(“listName='listName Here')。首先!
试试看!写吧{
list.items.append(newItem)
更新数据()
}
}
func updateData(){
allItems=realm.objects(Item.self)
tableListOfItems.reloadData()
}
///这将删除域中不是什么的所有项
///我想。我只想删除属于我的项目
///一个特定的列表。我试着。。。
///让list=realm.objects(ItemList.self).filter(“listName='defaultlist')。首先!
///list.items.removeAll()
@iAction func deleteAllItems(uSender:Any){
试试看!写吧{
realm.delete(realm.objects(Item.self))
更新数据()
}
}
func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
返回allItems.count
}
func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
let cell=tableView.dequeueReusableCell(标识符为:“myCell”,表示:indexath)
让data=allItems[indexPath.row]
cell.textLabel?.text=data.productName
返回单元
}
func tableView(tableView:UITableView,commit editingStyle:UITableViewCellEditingStyle,forRowAt indexPath:indexPath){
如果editingStyle==UITableViewCellEditingStyle.delete{
如果let item=allItems?[indexPath.row]{
试试看!写吧{
realm.delete(项目)
}
tableView.deleteRows(位于:[indexPath],带有:UITableViewRowAnimation.automatic)
}
}
}
}

在deleteAllItems函数中,您应该过滤它们。在不进行过滤的情况下,这是预期的行为,因为您只指定了一个对象类,所以Realm显然会删除与该类匹配的所有对象

如果要从域本身删除列表中包含的所有项,但保留空列表,则需要按以下方式更改deleteAllItems函数:

@IBAction func deleteAllItems(_ sender: Any) {
       guard let listToDelete = realm.objects(ItemList.self).filter("listName = %@",listNameToDelete).first else { return }
       try! realm.write {
           for item in listToDelete.items {
               realm.delete(realm.objects(Item.self).filter("productName = %@", item.productName).first)
               updateData()
           }
       }
    }
listNameToDelete应该在类中的函数外部声明,也可以在函数内部声明,具体取决于您实际想要实现什么。
此外,此实现假定您的ListName和ProductName是唯一的。

非常感谢您的帮助。仅供参考-我必须对in
循环中的
进行以下更改才能使其正常工作<代码>用于(listToDelete?.items)中的项目@Dávid Pászor-快速提问,
“listName=%@”
中的
=%@
做了什么?我已经更新了我的答案以安全地打开列表,您不应该使用强制展开,因为如果列表不存在,您的代码将崩溃。“%@是对象值的var arg替换,通常是字符串、数字或日期”-来自
class Item: Object {
    dynamic var productName = ""
    dynamic var createdAt = NSDate()
}
class ViewController: UIViewController, UITableViewDataSource{

    @IBOutlet weak var tableListOfItems: UITableView!
    @IBOutlet weak var inputProductName: UITextField!
    @IBOutlet weak var inputListName: UITextField!

    var allItems : Results<Item>!

    override func viewDidLoad() {
        super.viewDidLoad()
        updateData()
    }

    @IBAction func addNewList(_ sender: Any) {
        let list = ItemList()
        list.listName = inputListName.text!
        try! realm.write {
            realm.add(list)
        }
    }

    @IBAction func addNewItem(_ sender: Any) {
        let newItem = Item()
        newItem.productName = inputProductName.text!

        let list = realm.objects(ItemList.self).filter("listName = 'List Name Here'").first!

        try! realm.write {
            list.items.append(newItem)
             updateData()
        }
    }

    func updateData(){
        allItems = realm.objects(Item.self)
        tableListOfItems.reloadData()
    }

     /// This deletes every Item in Realm which is not what
     /// I want. I want to delete only items that belong to 
     /// a certain list. I tried...
    /// let list = realm.objects(ItemList.self).filter("listName = 'Default List'").first!
    /// list.items.removeAll()
    @IBAction func deleteAllItems(_ sender: Any) {
       try! realm.write {
           realm.delete(realm.objects(Item.self))
           updateData()
       }
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return allItems.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath)
        let data = allItems[indexPath.row]
        cell.textLabel?.text = data.productName
        return cell
    }

    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == UITableViewCellEditingStyle.delete{
            if let item = allItems?[indexPath.row] {
                try! realm.write {
                    realm.delete(item)
                }
                tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic)
            }
        }
    }
}
@IBAction func deleteAllItems(_ sender: Any) {
       guard let listToDelete = realm.objects(ItemList.self).filter("listName = %@",listNameToDelete).first else { return }
       try! realm.write {
           for item in listToDelete.items {
               realm.delete(realm.objects(Item.self).filter("productName = %@", item.productName).first)
               updateData()
           }
       }
    }