Search code examples
iosswiftsearchrealmdiacritics

Diacritic-insensitive filtering with Realm Swift


I have an app and I would like to know if it is possible that if I try to find a street based on its name, for example "ceska", my app can match a street whose name contains diacritics, such as "Česká".

I've tried using:

let predicate = NSPredicate(format: "name contains[c] %@", searchText)

When using name contains[d] it doesn't work.


Solution

  • There are three search variants of the lowercase letter č. There is the without-diacritics lowercase variant c, the uppercase variant Č and the uppercase without-diacritics variant C.

    Option d in contains controls the diacritics. Option c in contains controls case sensitivity.

    If you are looking for text ceska then:

    • contains will find only ceska (exact match)
    • contains[c] will find ceska and Ceska
    • contains[d] will find ceska and česka
    • contains[cd] will find ceska, česka, Ceska and Česka

    The diacritic-insensitive modifier is supported as of Realm Swift v2.5.0.