swiftuitableviewuisearchresultscontroller

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)

}

Solution

  • Don't use Objective-C stuff like NSPredicate and NSArray when you don't have to. Use Swift's filter and map.

    From what you described in the comments, you want to keep only the inner arrays that contains the searched text. You can do this:

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

    If you want to keep only the inner arrays that contains items that contains the search text, do this:

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