These are some String extensions that handle common scenarios gracefully. They are great fit for the Utilities module from the Modularization article.
Table of Contents
String Comparisons
The best way to compare strings is using the compare method. Using the diacriticInsensitive option, Search ignores diacritic marks (café == Cafe).\
This is really useful when adding a searchable component to a list.
extension String {
func matchesIgnoringCaseAndAccents(_ string: String) -> Bool {
lowercased().compare(string.lowercased(),options: .diacriticInsensitive) == .orderedSame
}
}
// Usage:
"café".matchesIgnoringCaseAndAccents("Cafe") // true
Automatic Plural handling
Handle plurals automatically using inflection in SwiftUI views:
Text("You've used ^[\(creditCount) credit](inflect: true) this month!")
// This will read:
// You've used 0 credits this month!
// You've used 1 credit this month!
// You've used 2 credits this month!