Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift: Filtering an array of structures using UISearchController/Predicates

Wondering if someone could help me out with filtering using predicates in Swift.

I have a somewhat messy datasource that I am using to populate a UITableView. The data source is an array of structures. The struct is defined as follows:

struct Exercises {
    let category: String
    let name : String
    let x_seed: [Double]
    let y_seed: [Double]
    let hasMult: Bool
}

Now in my tableview controller I'm holding an array of structures that contains all of the data for the table.

class MainTableViewController: UITableViewController, UISearchResultsUpdating {


var exercises = [Exercises]()
var filtered_exercises = [Exercises]()
var resultSearchController = UISearchController()

override func viewDidLoad() {
    super.viewDidLoad()

    // MARK: - Table view data source
    self.exercises = [
        Exercises(category:"Sports", name:"Bowling", x_seed:[125,155,185], y_seed:[3.00, 3.73, 4.43], hasMult:false),
        Exercises(category:"Sports", name:"Water Polo", x_seed:[125,155,185], y_seed:[10.00, 12.40,14.80], hasMult:false),
        Exercises(category:"Sports", name:"Handball", x_seed:[125,155,185], y_seed:[12.00, 14.87, 17.77], hasMult:false),
        Exercises(category:"Sports", name:"Dancing", x_seed:[125,155,185], y_seed:[3.00, 3.73, 4.43], hasMult:true),
        Exercises(category:"Sports", name:"Frisbee", x_seed:[125,155,185], y_seed:[3.00, 3.73, 4.43], hasMult:false),
        Exercises(category:"Sports", name:"Volleyball", x_seed:[125,155,185], y_seed:[3.00, 3.73, 4.43], hasMult:false),
        Exercises(category:"Sports", name:"Archery", x_seed:[125,155,185], y_seed:[3.50, 4.33, 5.17], hasMult:false),
        Exercises(category:"Sports", name:"Golf", x_seed:[125,155,185], y_seed:[3.50, 4.33, 5.17], hasMult:true)]

    self.resultSearchController = ({
        let controller = UISearchController(searchResultsController: nil)
        controller.searchResultsUpdater = self
        controller.dimsBackgroundDuringPresentation = false
        controller.searchBar.sizeToFit()

        self.tableView.tableHeaderView = controller.searchBar

        return controller
    })()

    self.tableView.reloadData()
}

What I would like to do is filter the exercises array based on the 'name' field and populate a new datasource filtered_exercises to fill the tableview. I am not sure how to wrap my head around how to use predicates in this situation.

// Search functionality 
func updateSearchResultsForSearchController(searchController: UISearchController)
{
    filtered_exercises.removeAll(keepCapacity: false)

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

    self.tableView.reloadData()
}

I know I could read all of the names in a string array and display that in the tableview easily. The problem is that I need to preserve the structures as the data contained there is passed onto other view controllers.

So, How can I filter an array of structs?

Thanks!

like image 235
0000101010 Avatar asked Oct 20 '22 07:10

0000101010


1 Answers

If you do not insist on NSPredicate (don't see a reason why you should since you're not using NSFetchRequest, ...), here's the code:

struct Exercises {
  let category: String
  let name : String
  let x_seed: [Double]
  let y_seed: [Double]
  let hasMult: Bool
}

let exercises = [
  Exercises(category:"Sports", name:"Bowling", x_seed:[125,155,185], y_seed:[3.00, 3.73, 4.43], hasMult:false),
  Exercises(category:"Sports", name:"Water Polo", x_seed:[125,155,185], y_seed:[10.00, 12.40,14.80], hasMult:false),
  Exercises(category:"Sports", name:"Handball", x_seed:[125,155,185], y_seed:[12.00, 14.87, 17.77], hasMult:false),
  Exercises(category:"Sports", name:"Dancing", x_seed:[125,155,185], y_seed:[3.00, 3.73, 4.43], hasMult:true),
  Exercises(category:"Sports", name:"Frisbee", x_seed:[125,155,185], y_seed:[3.00, 3.73, 4.43], hasMult:false),
  Exercises(category:"Sports", name:"Volleyball", x_seed:[125,155,185], y_seed:[3.00, 3.73, 4.43], hasMult:false),
  Exercises(category:"Sports", name:"Archery", x_seed:[125,155,185], y_seed:[3.50, 4.33, 5.17], hasMult:false),
  Exercises(category:"Sports", name:"Golf", x_seed:[125,155,185], y_seed:[3.50, 4.33, 5.17], hasMult:true)
]

let options = NSStringCompareOptions.CaseInsensitiveSearch | NSStringCompareOptions.DiacriticInsensitiveSearch

// Filter exercises by name (case and diacritic insensitive)
let filteredExercises = exercises.filter {
  $0.name.rangeOfString("Ol", options: options) != nil
}

let filteredExerciseNames = ", ".join(filteredExercises.map({ $0.name }))
println(filteredExerciseNames)

It prints Water Polo, Volleyball, Golf.

like image 96
zrzka Avatar answered Oct 22 '22 03:10

zrzka