swift 3 - 搜索结果也带有变音符号 [英] swift 3 - search result also with diacritics

查看:18
本文介绍了swift 3 - 搜索结果也带有变音符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我使用的搜索效果很好,但我需要建议.当我搜索没有变音符号的文本时,我不知道该怎么做,结果也带有变音符号.示例:输入搜索holesovice,我需要找到holesoviceholešovice.

In my application I use a search that works well but I need advice. I do not know how to do that when I search for text without diacritics that the result also with diacritics. Example: Enter a search holesovice, I need to find holesovice and also holešovice.

这是我的部分代码:

func filterContentForSearchText(_ searchText: String) {
        filteredERequests = requests.filter({( request : CityRequest) -> Bool in

            return request.cityName.lowercased().contains(searchText.lowercased())

        })
        myTable.reloadData()
    }

推荐答案

您可以将 range(of:, options:) 与不区分变音符号的选项一起使用(以及可选的不区分大小写)搜索.示例:

You can use range(of:, options:) with options for a diacritic insensitive (and optionally case insensitive) search. Example:

let list = ["holesovice", "holešovice"]
let searchTerm = "sovi"

let filtered = list.filter {
    $0.range(of: searchTerm, options: [.diacriticInsensitive, .caseInsensitive]) != nil
}

print(filtered) // ["holesovice", "holešovice"]

这篇关于swift 3 - 搜索结果也带有变音符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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