Search code examples
iosswiftnspredicatecloudkit

Predicate comparing two keys


I am trying to write a predicate to compare two keys in a cloud kit database. All the documentation I've been able to find only allows you to compare one key against a set value. Is there a way to write a predicate in swift to compare two keys?

The closest I've been able to come up with is:

 let pred = NSPredicate(format: "%K == %K", "keyA", "keyB")!

but that keeps crashing on me.


Solution

  • use %@ on R.H.S side insted of %k like this..

    %@ is a var arg substitution for an object value—often a string, number, or date.

    %K is a var arg substitution for a key path.

    let pred = (NSPredicate(format: "%K == %@", "keyA", "keyB")!)