Searching through objects in array swift

佐手、 提交于 2019-12-21 04:36:08

问题


I'm trying to create a search function using the UISearchController. However i cant seem to make it work with my Team Object. I've started by creating a Team Object which contain a id, name and shortname. Then i'm retrieving the teamData from a url and adding the Team Objects into an array which is populated into a tableView. This tableView contain a searchController which is suppose to filter the 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

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

}

}

回答1:


The objects in the teamArray don't have a SELF property. You can't use SELF to search in all the properties of the object at once. You have to give the name of the property, and if you want to search in more than one you have to add all those properties to the predicate.

I would think it's enough for you to search in the name property like so:

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

If you need in more properties you do like this:

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



回答2:


Can you post the 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 haven't used NSPRedicate a lot, but my understanding of the CONTAINS comparison is that it checks an individual field to see if it contains a substring. I don't think it will check all fields of the objects you're searching. That seems like what you're expecting.




回答3:


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


来源:https://stackoverflow.com/questions/29940885/searching-through-objects-in-array-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!