Search code examples
iosswiftnspredicate

Who can explain this NSPredicate syntax


I have this predicate:

let searchPredicate = NSPredicate(format: "SELF LIKE[cd] %@", searchString)

which I'm using for filtering array and finding the needed string.

Could you explain what this argument "SELF LIKE[cd] %@" means?


Solution

  • To start with, NSPredicate(format:) is like String(format:), so the %@ is replaced with the contents of searchString.

    [cd] means case and diacritic insensitive - so john Jonés would match John Jones

    LIKE is used to match with a wildcard (* = 0 or more chars, ? = 1 character). so LIKE Joh* would match with John Jones