Swift - 根据多个条件搜索大数组

Swift - Search large array based on multiple conditions

我有一个包含 4K+ 项的数组,使用 UISearchBar 和 UISearchController 对列表进行排序以查找匹配项。我想按几个标准对数组进行排序,但最重要的是首先按用户类型排序

搜索控制器有 3 个范围按钮

搜索数组中的项目正在使用结构 class 访问条件:

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

这在项目很少的情况下很好,但如果我有大数组并且在使用上面的块时,将包括大量不相关的项目,其中包含键入的字符串。

我可以使用谓词通过 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()
}

请问如何处理满足类别以及输入字符串匹配和字符串(第一个单词)length/range 的逻辑?

谢谢

我找到路了。我结合了 .filter 和 .sort 得到结果:

func filterContentForSearchText(_ searchText: String, scope: String = "All") {

    let options = NSString.CompareOptions.caseInsensitive

    if scope == "All"{

        print("filtering All")
        self.filteredItems = allItems
            .filter{[=10=].title.range(of: searchText, options: options) != nil && [=10=].allCat == scope}
            .sorted{ ([=10=].title.hasPrefix(searchText) ? 0 : 1) < (.title.hasPrefix(searchText) ? 0 : 1) }
    }
    else{

        print("filtering \(scope)")
        self.filteredItems = allItems
            .filter{[=10=].title.range(of: searchText, options: options) != nil && [=10=].category == scope}
            .sorted{ ([=10=].title.hasPrefix(searchText) ? 0 : 1) < (.title.hasPrefix(searchText) ? 0 : 1) }

    }
    tableView.reloadData()
}

希望这对某人有所帮助