ios - 在 Swift 3 中匹配和删除数组项

标签 ios arrays swift

我有 NSMutableDictionary,我想删除 并刷新我的 TableView ,其中 code 值等于 deletecode.

How can I do it? , My codes under below.

var myCells = [NSMutableDictionary]()
myCells = [["isExpanded" : false,
            "isVisible" : true,
            "additionalRows" : 7,
            "code" : "Flex1"
           ],
           [ "isExpanded" : false,
             "isVisible" : true,
             "additionalRows" : 7,
             "code" : "Flex3"
           ],
           [ "isExpanded" : false,
             "isVisible" : true,
             "additionalRows" : 7,
             "code" : "Flex4"
           ]]

 let deletecode = "Flex1"

 if let indexOfA = myCells.flatMap({$0["\(deletecode))"]}).last {
     self.tableView.beginUpdates()
     self.myCells.remove(at: indexOfA as! Int)
     self.tableView.endUpdates()
     self.tableView.reloadData()
 }

我的代码第 2 部分

myCells = myCells.filter { (element) -> Bool in
        if let code = element["additionalRows"] as? String {
            if code != "7" {

            }else {

                let fetchRequestf                = NSFetchRequest<NSFetchRequestResult>(entityName:"Match")
                let predicatek = NSPredicate (format:"code = %@",(element["code"] as? String)!)
                fetchRequestf.predicate = predicatek

                do {
                    let getSett = try context.fetch(fetchRequestf) as! [Match]

                    if (getSett.count > 0) {

                        if (getSett[0].show == false){

                            let deletecode = getSett[0].code

                            self.tableView.beginUpdates()
                            myCells = myCells.filter { (element) -> Bool in
                                if let code = element["code"] as? String {
                                    return code != deletecode
                                }
                                return true
                            }

                            self.tableView.endUpdates()
                            self.tableView.reloadData()
                        }
                    }


                } catch let error as NSError {
                    print(error)
                }
            }
        }

        return true

    }

最佳答案

您可以简单地过滤您的数组:

myCells = myCells.filter { (element) -> Bool in
    if let code = element["code"] as? String {
        return code != deletecode
    }
    return true
}
self.tableView.reloadData()

关于ios - 在 Swift 3 中匹配和删除数组项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43466965/

相关文章:

python - 我如何在Python中将这个字符串变成2个数组?

php - 从字符串生成数组

swift - 在 Swift 4 模式下使用 Swift 3 @objc 推理是否被弃用?

ios - 更改 UISearchBar 搜索字段背景

ios - ALAssetsLibrary 图像扭曲

java - 为什么我不能在 Java 中构造这样的字符串数组?

swift - 代码 8 : Cannot inherit class from the framework

swift - Swift 中的错误处理不涉及堆栈展开。这是什么意思?

iOS 14 链接模块标志 'Dwarf Version' : IDs have conflicting behaviors

ios - 禁用 iPhone 键盘按钮