ios - Swift: Filtering an array of structures using UISearchController/Predicates -
wondering if me out filtering using predicates in swift.
i have messy datasource using populate uitableview. data source array of structures. struct defined follows:
struct exercises { let category: string let name : string let x_seed: [double] let y_seed: [double] let hasmult: bool }
now in tableview controller i'm holding array of structures contains of data 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 filter exercises array based on 'name' field , populate new datasource filtered_exercises fill tableview. not sure how wrap head around how use predicates in 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 read of names in string array , display in tableview easily. problem need preserve structures data contained there passed onto other view controllers.
so, how can filter array of structs?
thanks!
if not insist on nspredicate
(don't see reason why should since you're not using nsfetchrequest
, ...), here's 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 name (case , 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.
Comments
Post a Comment