如何搜索[[String]]类型的字符串或字符

时间:2019-05-17 11:40:38

标签: swift uitableview uisearchresultscontroller

//Am not getting any search list of cells
//when am searching am getting empty array every time.

var actors = [["a0","b0","c0","d0"],["a1","b1","c1","d1"],["a2","b2","c2","d2"],["a3","b3","c3","d3"]]

var filterArr:[[String]] = []

func updateSearchResults(for searchController: UISearchController) {        
    print("Searching for text:----\(searchController.searchBar.text!)")

    let predicate = NSPredicate(format: "SELF Contains[c] %@" , searchController.searchBar.text!)

    filterArr = (actors as NSArray).filtered(using: predicate) as! [[String]]
    print(filterArr)

}

1 个答案:

答案 0 :(得分:0)

不必使用NSPredicateNSArray之类的Objective-C东西。使用Swift的filtermap

根据您在注释中描述的内容,您只想保留包含搜索文本的内部数组。您可以这样做:

filterArr = actors.filter { $0.contains(searchController.searchBar.text!) }

如果只想保留包含 搜索文本的项目的内部数组,请执行以下操作:

filterArr = actors.filter { $0.contains { $0.contains(searchController.searchBar.text!) } }