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 diacriti
This is Martin R's solution, just easier to consume.
Copy this into a Swift file:
extension String {
func contains(insensitive other: String, range: Range<String.Index>? = nil, locale: Locale? = nil) -> Bool {
return self.range(of: other, options: [.diacriticInsensitive, .caseInsensitive], range: range, locale: locale) != nil
}
}
Use it like this:
if "Música".contains(insensitive: "music") {
print("hurray!")
}
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"]