Pedro de Sá Pedro de Sá - 1 year ago 52
Swift Question

How to set NSPredicate to look for fields different than nil?

I'm trying to fetch fields in CloudKit that are different than nil and, also, that has a specific phone number (at the same time, obviously). But I'm having a hard time trying to do so... That is my code:

let predicate = Predicate(format: "phone == %@ && careTakerId != %@", tel, "")

If exists, it prints a "User exists" and it's printing it every damn time! Can anyone tell me if I'm doing the predicate right? Thank you

Answer Source

Got it by doing the following:

let predicate = Predicate(format: "phone == %@ && careTakerId != ''", tel)

If you try with != nil Xcode will crash and say it's not a valid predicate!