Search code examples
swiftnspredicate

How to use NSPredicate to return true?


Here is my code:

let newPredicate = NSPredicate(format: "true == %@")        
let y = newPredicate.evaluate(with: true)

print(y)

It always returns false.

How to use NSPredicate to return true?


Solution

  • The %@ placeholder is for arguments passed to the NSPredicate(format:) call, like this:

    let newPredicate = NSPredicate(format: "true == %@", true)
    

    If you want to use the object passed to evaluate(with:), use self:

    let newPredicate = NSPredicate(format: "true == self")
    let y = newPredicate.evaluate(with: true)
    print(y)