Pedro de Sá Pedro de Sá - 4 months ago 13
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

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!