首页 > 解决方案 > 使用三个键 Swift 5 过滤 Firebase QueryDocumentSnapshot 数组

问题描述

我已经对一个简单的数组应用了过滤器,但现在数据在QueryDocumentSnapshot数组中我无法过滤它,它应该用 3 个键过滤phone,,nameemail

extension CleanerListViewController: UISearchBarDelegate{
    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {

        if searchText.isEmpty{
             filterList = self.cleaner
        }else{
            let keys = ["name", "phone", "email"]
            let subPredicates = keys.map({ NSPredicate(format: "%K CONTAINS[c] %@", $0, searchText) })
            let predicate = NSCompoundPredicate(orPredicateWithSubpredicates: subPredicates)
           // cleaner is of type [QueryDocumentSnapshot]!
            filterList = cleaner.filter { predicate.evaluate(with: $0) }

        }
        self.tblCleanerList.reloadData()
    }
}

我已经尝试过.filter一系列[QueryDocumentSnapshot]但无法弄清楚该怎么做

它会导致崩溃

 *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<FIRQueryDocumentSnapshot 0x600002b00580> valueForUndefinedKey:]: this class is not key value coding-compliant for the key name.'

标签: iosswiftfirebasefiltersnapshot

解决方案


推荐阅读