updateSearchResultsForSearchController JSON

336 Views Asked by At

I try to implement search to my tableview, I got the following error. How I am going to implement for JSON data?

var articleList: [JSON]? = []
var searchResults: [JSON]? = []

And my updateSearchResultsForSearchController function :

func updateSearchResultsForSearchController(searchController: UISearchController) {
    self.searchResults?.removeAll(keepCapacity: false)

    let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text)

    let array = (articleList as NSArray).filteredArrayUsingPredicate(searchPredicate)
    searchResults = array as! [String]

    self.tableView.reloadData()
}

ERROR : '[JSON]?' is not convertible to 'NSArray'

Thanks...

1

There are 1 best solutions below

2
On BEST ANSWER

Make (instead of cast) a new NSArray from your Swift array:

let nsArticleList = NSArray(array: articleList!)

Then use it:

let array = nsArticleList.filteredArrayUsingPredicate(searchPredicate)