如何使用搜索栏过滤json数据?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (54)
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String)
{

    searchBar.barTintColor = UIColor.white
    let lists:Array = searchCollectionList1 as Array
   // let searchPredicate = NSPredicate(format: "assetname CONTAINS[C] %@", searchText)
    let searchPredicate = searchFilter(searchName: searchText) 'Constant 'searchPredicate' inferred to have type '()', which may be unexpected'
    let array = (lists as NSArray).adding(searchPredicate)
    filteredDataItems = array

    DispatchQueue.main.async {
         self.collectionView.reloadData()
    }


}
提问于
用户回答回答于

不要使用NSPredicate,使用原生filter

range(of ... ) != nil和选项.caseInsensitiveCONTAINS[C]完全相同。

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

    searchBar.barTintColor = UIColor.white
    filteredDataItems = searchCollectionList1.filter{ $0.assetname.range(of: searchText, options: .caseInsensitive) != nil }
    DispatchQueue.main.async {
        self.collectionView.reloadData()
    }
}

这假定searchCollectionList1filteredDataItems是相同的类型。

扫码关注云+社区

领取腾讯云代金券