I have an array of strings dictionaryStrings
filled with Vietnamese phrases. When a user inputs a search word, I would like to see if that userInput
is contained as a substring in any of the array elements. This works if user types the word with the correct Vietnamese diacritics/accent marks. (e.g. typing "chân" will match with one of the array elements "chân"). But I would also like it to match if user types simply "chan", and will match with "chân". I found in this answer that string.stringByFoldingWithOptions
can remove diacritics. But after importing Foundation
I still get Value of type 'String' has no member 'stringByFoldingWithOptions'
error. How can I do this with string.contains()
?
userInput = searchViewModel.text.lowercased()
if !userInput.isEmpty {
filteredDictionary = dictionaryStrings.filter {
$0.lowercased().contains(userInput)
}
}
stringByFoldingWithOptions is a member of NSString in Objective-C.
For Swift, you may want to look into the equivalent folding method.
Some of the latest answers at the link you provided give examples on using folding(options:locale:):
func toNoSmartQuotes() -> String {
let userInput: String = self
return userInput.folding(options: .diacriticInsensitive, locale: .current)
}