如何搜索 [[String]] 类型的字符串或字符
How to search a string or character which is in [[String]] type
//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)
}
不要在不必要时使用 Objective-C 之类的东西,例如 NSPredicate
和 NSArray
。使用 Swift 的 filter
和 map
.
根据您在评论中的描述,您只想保留包含搜索文本的内部数组。你可以这样做:
filterArr = actors.filter { [=10=].contains(searchController.searchBar.text!) }
如果您只想保留包含 包含 搜索文本的项目的内部数组,请执行以下操作:
filterArr = actors.filter { [=11=].contains { [=11=].contains(searchController.searchBar.text!) } }
//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)
}
不要在不必要时使用 Objective-C 之类的东西,例如 NSPredicate
和 NSArray
。使用 Swift 的 filter
和 map
.
根据您在评论中的描述,您只想保留包含搜索文本的内部数组。你可以这样做:
filterArr = actors.filter { [=10=].contains(searchController.searchBar.text!) }
如果您只想保留包含 包含 搜索文本的项目的内部数组,请执行以下操作:
filterArr = actors.filter { [=11=].contains { [=11=].contains(searchController.searchBar.text!) } }