斯威夫特:使用过滤结构数组UISearchController / predicates [英] Swift: Filtering an array of structures using UISearchController/Predicates
问题描述
想知道,如果有人可以帮助我使用predicates斯威夫特过滤。
Wondering if someone could help me out with filtering using predicates in Swift.
我有我使用填充一个UITableView一个有些凌乱的数据源。数据源是结构的数组。结构体定义如下:
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
}
现在在我的tableview控制器我抱着结构的数组包含所有表的数据。
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()
}
我想要做的就是过滤的练习的基础上,姓名字段阵列和填充新的数据源filtered_exercises填补的tableview。我不知道如何包装我围绕如何在这种情况下使用predicates头。
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()
}
我知道我可以读取所有的名字在一个字符串数组,并显示在tableview中容易。问题是,我需要preserve结构作为数据包含有被传递到其他视图控制器。
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?
谢谢!
推荐答案
如果你不坚持 NS predicate
(不明白了一个道理,为什么你应该因为你不使用 NSFetchRequest
,...),这里的code:
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)
它打印的水球,排球,高尔夫球的
这篇关于斯威夫特:使用过滤结构数组UISearchController / predicates的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!