Search code examples
swift3nspredicate

NSPredicate Differences


I have been teaching myself predicates and I have a situation with differing behavior between two predicates that should act the same.

This first case works correctly, returning some items:

let wordPredicate = NSPredicate(format: "name CONTAINS[cd] 'word'")
(items as NSArray).filtered(using: wordPredicate)

bet the second case does not return anything:

let keyContainsVarPredicate = NSPredicate(format: "$key CONTAINS[cd] '$word'")
(items as NSArray).filtered(using: keyContainsVarPredicate.withSubstitutionVariables(["key": "name", "word": "word"])

Solution

  • I figured it out actually.

    You cannot substitute in you property name.