我在我的应用程序中已经按照以下示例代码搜索功能 https://www.raywenderlich.com/157864/uisearchcontroller-tutorial-getting-started
一切都很好,搜索工作也很完美。但是当我以不同的顺序/顺序输入单词时,它不会给我结果。下面是我想要的例子
var String = "High, Fever"
var String = "fever"
现在,当我搜索"发烧"它给了我两个回应,但当我搜索类似"发烧h"它没有给我字符串..在我的情况下,他们只是输入不管他们的顺序的单词..
以下是我的代码
func filterContentForSearchText(_ searchText: String, scope: String = "Present") {
filteredsymptoms = symptoms.filter({( symptoms : Symptoms) -> Bool in
let doesCategoryMatch = (scope == "Present") || (scope == "Absent") || (scope == "Focus")
if searchBarIsEmpty() {
return doesCategoryMatch
} else {
return doesCategoryMatch && symptoms.name.lowercased().contains(searchText.lowercased())
}
})
print(filteredsymptoms)
self.tblListOfSymptoms.reloadData()
}
答案 0 :(得分:0)
symptoms.name.lowercased().contains(searchText.lowercased()
这部分代码将检查“symptoms.name”是否包含整个搜索文本,在您的情况下是“fever h”。 如果要搜索以空格分隔的文本的所有部分,则应使用以下内容:
func filterContentForSearchText(_ searchText: String, scope: String = "Present") {
filteredsymptoms = symptoms.filter({( symptoms : Symptoms) -> Bool in
let doesCategoryMatch = (scope == "Present") || (scope == "Absent") || (scope == "Focus")
if searchBarIsEmpty() {
return doesCategoryMatch
} else {
var result:Bool = false
let searchTerms = searchText.lowercased().components(separatedBy: CharacterSet.whitespacesAndNewlines)
for searchTerm in searchTerms
{
result = doesCategoryMatch && symptoms.name.lowercased().contains(searchTerm)
if result == true
{
break
}
}
return result
}
})
print(filteredsymptoms)
self.tblListOfSymptoms.reloadData()
}
答案 1 :(得分:0)
我想这取决于你想要达到的结果。如果您希望用户键入多个单词并搜索其中任何一个单词,请使用以下代码:
searchText.lowercased().components(separatedBy: " ").map({ symptoms.name.lowercased().contains($0) }).reduce(false, { $0 || $1 })