I'm trying to filter a JSON populated UITableView and so far I have tried 3 or 4 pretty nice tutorials but I am getting the same error on all of them when I press the SearchBar.
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Can't use in/contains operator with collection MakeItWork.Repository (not a collection'
The only thing that is different from the tutorials I have seen is that my Array is on another file and I am calling it from there.
Repository.swift
class Repository {
var name: String?
var releaseDate: String?
var gameImage: String?
var id: String?
init(json: NSDictionary) {
self.name = json["name"] as? String
self.releaseDate = json["release_date"] as? String
self.gameImage = json["image"] as? String
self.id = json["_id"] as? String
}
}
And the GameTableViewController.swift
var games = [Repository]()
var filteredTableData = [String]()
var shouldShowSearchResults = false
var searchController: UISearchController!
func configureSearchController() {
// Initialize and perform a minimum configuration to the search controller.
searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search here..."
searchController.searchBar.delegate = self
searchController.searchBar.sizeToFit()
// Place the search bar view to the tableview headerview.
self.tableView.tableHeaderView = searchController.searchBar
}
func updateSearchResultsForSearchController(searchController: UISearchController) {
let searchString = searchController.searchBar.text!
filteredTableData.removeAll(keepCapacity: false)
let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@",searchString)
let array = (games as NSArray).filteredArrayUsingPredicate(searchPredicate)
filteredTableData = array as! [String]
self.tableView.reloadData()
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if shouldShowSearchResults {
return filteredTableData.count
}
else {
return games.count
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "GameTableViewCell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! GameTableViewCell
if (shouldShowSearchResults) {
cell.nameLabel?.text = filteredTableData[indexPath.row]
return cell
}
else {
cell.nameLabel?.text = games[indexPath.row].name
cell.releaseDateLabel?.text = games[indexPath.row].releaseDate
if let url = NSURL(string: self.games[indexPath.row].gameImage!) {
cell.photoImageView?.hnk_setImageFromURL(url)
}
}
return cell
}
I put a break point at the start of updateSearchResultsForSearchController and I noticed that every time I press the UISearchBar, before the crash, it does a loop of searching with a null searchString since I haven't typed anything inside yet. Is that normal? Last tutorial I checked was http://www.ioscreator.com/tutorials/add-search-table-view-tutorial-ios8-swift . What am I missing here ?
Edit: I think it is normal for the UISearchBar to keep updating all the time as I am calling the UISearchResultsUpdating.