Finding objects in a swift array

I am trying to create a search function using UISearchController. However, I cannot get it to work with my Team object. I started by creating a Team Object that contains an identifier, name, and short name. Then I extract teamData from the url and add Team Objects to the array that is populated in the tableView. This tableView contains a searchController that should filter data, but nothing happens.

arrays

var teamArray = Array<Team>() var filteredTableData = [String]() 

Getteams function

 func getTeams(url: String) { isApiCalling = true request(.GET, url, parameters: nil) .response { (request, response, data, error) in if error == nil { let data: AnyObject = data! let jsonArray = JSON(data: data as! NSData) for (key: String, subJson: JSON) in jsonArray { // Create an object and parse your JSON one by one to append it to your array var newTeamObject = Team(id: subJson["id"].intValue, name: subJson["name"].stringValue, shortname: subJson["shortname"].stringValue) self.teamArray.append(newTeamObject) } self.isApiCalling = false self.tableView.reloadData() self.refreshControl?.endRefreshing() } } } 

CellForRowAtIndexPath

 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("teamCell", forIndexPath: indexPath) as! TeamCell cell.textLabel?.font = UIFont(name: "HelveticaNeue-Light", size: 20) cell.textLabel?.text = self.teamArray[indexPath.row].name as String if (self.cellSelected.containsObject(indexPath)) { cell.accessoryView = cell.accessoryCheck } else { cell.accessoryView = cell.accessoryUncheck } return cell } 

FilterData p>

 func updateSearchResultsForSearchController(searchController: UISearchController) { filteredTableData.removeAll(keepCapacity: false) let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text) let array = (teamArray as NSArray).filteredArrayUsingPredicate(searchPredicate) filteredTableData = array as! [String] self.tableView.reloadData() } 

Team objects

 class Team{ var id: Int! var name: NSString! var shortname: NSString! init(id: Int, name:NSString, shortname: NSString) { self.id = id self.name = name self.shortname = shortname } } 
+7
ios uitableview swift uisearchcontroller
source share
3 answers

Objects in teamArray do not have a SELF property. You cannot use SELF to search all the properties of an object at the same time. You must specify the name of the property, and if you want to search for more than one, you must add all these properties to the predicate.

I would think that you just need to find the name in the property like this:

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

If you need more properties, you like it:

 let searchPredicate = NSPredicate(format: "name CONTAINS[c] %@ OR shortname CONTAINS[c] %@", searchController.searchBar.text, searchController.searchBar.text) 
+7
source share

Can you post a definition of your Team object and any sub-objects that it contains? (TeamData).

Also indicate where you expect the search text to appear in your team object.

I have not used NSPRedicate much, but my understanding of the CONTAINS comparison is that it checks a single field to see if it contains a substring. I do not think he will check all the fields of the objects you are looking for. This is similar to what you expect.

+2
source share
 let searchPredicate = NSPredicate(format: "name contains[c] %@", searchWord) self.filteredArray = self.array.filteredArrayUsingPredicate(searchPredicate) 
+2
source share

All Articles