我有一个包含 4K+ 项的数组,使用 UISearchBar 和 UISearchController 对列表进行排序以查找匹配项。我想按几个标准对数组进行排序,但最重要的是首先按用户类型排序
搜索 Controller 有 3 个范围按钮用于
- 类别
- 子类别
- 所有的猫
搜索数组中的项目正在使用结构类来访问条件:
struct Item {
var title: String
var category: String
var subCategory: String
var allCat: String
}
过滤的标准方法是使用这样的方法:
func filterContentForSearchText(_ searchText: String, scope: String = "All") {
self.filteredItems = allItems.filter({( item : Item) -> Bool in
let categoryMatch = (item.allCat == scope) || (item.category == scope)
return categoryMatch && item.title.lowercased().contains(searchText.lowercased())
//return categoryMatch && (item.title.lowercased().range(of:searchText.lowercased()) != nil)
})
tableView.reloadData()
}
这在项目很少的情况下没问题,但如果我有大数组并且在使用上面的 block 时,将包括大量不相关的项目,其中包含键入的字符串。
我可以使用谓词通过 BEGINSWITH 获得更好的结果,但我无法满足类别的条件。我很确定下面的代码块无效,希望能提供更多经济建议。另一个障碍是该数组包含具有多个单词的字符串。例如:
array = ["Apple", "Apple Freshly Picked", "Apple Green", "Pear", "Melon", "Pear Yellow",....]
所以当用户开始输入“Ap”时的结果应该是
- 苹果
- 苹果绿
- 新鲜采摘的苹果
我有点让它在没有满足搜索类别(结构)条件的情况下工作:
func filterContentForSearchText(_ searchText: String, scope: String = "All") {
let words = searchText.components(separatedBy: " ")
var word1 = ""
var word2 = ""
var word3 = ""
var predicate = NSPredicate()
var array = [Item]()
if scope == "All" {
if words.count == 1{
word1 = (words[0])
predicate = NSPredicate(format: "SELF BEGINSWITH[cd] %@ OR SELF LIKE[cd] %@", word1, word1)
}
else if words.count == 2{
word1 = (words[0])
word2 = (words[1])
predicate = NSPredicate(format: "SELF BEGINSWITH[cd] %@ AND SELF CONTAINS[cd] %@", word1, word2)
}
else if words.count == 3{
word1 = (words[0])
word2 = (words[1])
word3 = (words[2])
predicate = NSPredicate(format: "SELF BEGINSWITH[cd] %@ AND SELF CONTAINS[cd] %@ AND SELF CONTAINS[cd] %@", word1, word2, word3)
}
} else {
predicate = NSPredicate(format: "title BEGINSWITH[cd] %@ AND category == %@", searchText, scope)
if words.count == 1{
word1 = (words[0])
predicate = NSPredicate(format: "SELF BEGINSWITH[cd] %@ OR SELF LIKE[cd] %@ AND category == %@", word1, word1, scope)
}
else if words.count == 2{
word1 = (words[0])
word2 = (words[1])
predicate = NSPredicate(format: "SELF BEGINSWITH[cd] %@ AND SELF CONTAINS[cd] %@ AND category == %@", word1, word2, scope)
}
else if words.count == 3{
word1 = (words[0])
word2 = (words[1])
word3 = (words[2])
predicate = NSPredicate(format: "SELF BEGINSWITH[cd] %@ AND SELF CONTAINS[cd] %@ AND SELF CONTAINS[cd] %@ AND category == %@", word1, word2, word3, scope)
}
}
array = (allItems as NSArray).filtered(using: predicate) as! [Item]
self.filteredItems = array
let lengthSort = NSSortDescriptor(key: "length", ascending: true)
let sortedArr = (self.filteredItems as NSArray).sortedArray(using: [lengthSort])
self.filteredItems = sortedArr as! [Item]
self.tableView.reloadData()
}
请问我如何处理满足类别以及输入字符串匹配和字符串(第一个单词)的长度/范围的逻辑?
谢谢
最佳答案
我找到了路。我结合了 .filter 和 .sort 得到结果:
func filterContentForSearchText(_ searchText: String, scope: String = "All") {
let options = NSString.CompareOptions.caseInsensitive
if scope == "All"{
print("filtering All")
self.filteredItems = allItems
.filter{$0.title.range(of: searchText, options: options) != nil && $0.allCat == scope}
.sorted{ ($0.title.hasPrefix(searchText) ? 0 : 1) < ($1.title.hasPrefix(searchText) ? 0 : 1) }
}
else{
print("filtering \(scope)")
self.filteredItems = allItems
.filter{$0.title.range(of: searchText, options: options) != nil && $0.category == scope}
.sorted{ ($0.title.hasPrefix(searchText) ? 0 : 1) < ($1.title.hasPrefix(searchText) ? 0 : 1) }
}
tableView.reloadData()
}
希望对大家有帮助
关于ios - Swift - 根据多个条件搜索大数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45521911/