Swift:使用UISearchController / Predicates过滤结构数组

想知道是否有人可以帮助我在Swift中使用谓词进行过滤。

我有一个有点凌乱的数据源,我正在使用填充UITableView。 数据源是一个结构数组。 结构定义如下:

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

现在在我的tableview控制器中,我持有一个包含表的所有数据的结构数组。

 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() } 

我想要做的是过滤基于'name'字段的Exercises数组,并填充新的数据源filtered_exercises来填充tableview。 我不确定如何在这种情况下如何使用谓词。

 // Search functionality func updateSearchResultsForSearchController(searchController: UISearchController) { filtered_exercises.removeAll(keepCapacity: false) let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text!) // ?????????????? self.tableView.reloadData() } 

我知道我可以读取string数组中的所有名称,并轻松地在tableview中显示。 问题是我需要保留结构,因为包含在其中的数据被传递到其他视图控制器。

那么,如何过滤一个结构数组?

谢谢!

如果你不坚持NSPredicate (因为你没有使用NSFetchRequest ,所以没有看到你应该这样做的原因),下面是代码:

 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) 

它打印水球,排球,高尔夫