在Swift 5中搜索多个单词而忽略顺序 [英] Search for multiple words ignoring the order in Swift 5

查看:72
本文介绍了在Swift 5中搜索多个单词而忽略顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我构建了一个SearchBar,对于单个单词来说,它工作正常。

In my app I built a SearchBar which, for a single word, works perfectly fine.

导入基金会

class Clinic {
    var id = ""
    var name = ""
    var address = ""
    var specialty1 = ""
    var specialty2 = ""
}

在我的textDidChange中,我

In my textDidChange I have

var clinics: [Clinic] = [] // Clinics Data Structure
var clinicsSearch: [Clinic] = [] // Filtered Clinics

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {

    // works for one word (either in name or specialty1 or specialty2)
    clinicsSearch = clinics.filter { $0.name.lowercased().contains(searchText.lowercased()) ||
        $0.specialty1.lowercased().contains(searchText.lowercased()) ||
        $0.specialty2.lowercased().contains(searchText.lowercased())
    }
    searching = true
    tableView.reloadData()
}

使用上述功能,如果我开始键入内容,它将带出所有符合条件的结果(如果键入的单词(在名称OR Specialty1或Specialty2中找到)。

With the func above, if I start typing something, it will bring all results that meet the criteria (if the typed word is found in the Name OR Specialty1 OR Specialty2).

我现在开始寻求改进代码,并希望实现一个用户可以键入单词的选项以不同的顺序,例如:命名SPACE speciality(1or2),specialty(1or2)SPACE Name等,应用程序将在所有3个字段中搜索所有键入的单词。独立的订单。

我在这里发现了一些与我要寻找的东西非常相似的东西,但是我在那里问不到,因为我的声誉低于50美元b $ b 搜索多个单词而忽略Swift中的顺序

I found here something very similar to what I'm looking for but I couldn't ask there as I'm below 50 reputation Search for multiple words ignoring the order in Swift

与此处介绍的解决方案的主要区别在于,它们在示例中使用了基本数组。

The main difference from the solution presented there is that they use basic a array in the example

let filterArray = ["Big green bubble", "Red bubble", "A bubble in green", "Small green bubble", "This bubble is green"]

与我的不同,它位于上面的Class Clinic中。我试图按照以下情况调整他们的代码

That is different from mine, that is based in the Class Clinic above. I tried to adapt their code to my case as below

let textString = searchText.lowercased()
let words = textString.components(separatedBy: " ")
clinicsSearch = clinics.map { $0.name.lowercased() }.filter { string in words.allSatisfy { string.components(separatedBy: " ").contains($0) } }

但是我遇到了一些错误,例如'Clinic'类型的Value没有成员'lowercased'或类型 Clinic的值没有我不知道如何解决的成员组件(还有其他组件)。

But I get several errors like Value of type 'Clinic' has no member 'lowercased' or Value of type 'Clinic' has no member 'components' (there are others as well) that I don't know how to fix.

任何帮助将不胜感激。

Any help is greatly appreciated.

谢谢

推荐答案

您可以尝试

let searArr = searchText.lowercased().components(separatedBy: " ")
clinicsSearch = clinics.filter { item in
                                  let res  = searArr.filter { item.name.lowercased().contains($0) ||    
                                  item.specialty1.lowercased().contains($0) || 
                                  item.specialty2.lowercased().contains($0) 
                }
   return !res.isEmpty
}

这样做可能更好

let searArr = searchText.lowercased().components(separatedBy: " ")
clinicsSearch = clinics.filter { item in
         let lowName = item.name.lowercased()
         let lowSp1 = item.specialty1.lowercased()
         let lowSp2 = item.specialty2.lowercased()
         let res  = searArr.filter {
                      lowName.contains($0) ||    
                      lowSp1.contains($0) || 
                      lowSp2.contains($0) 
         }
   return !res.isEmpty
}

这篇关于在Swift 5中搜索多个单词而忽略顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆