NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

SOLVED: Day 61: predicate problem

Forums > 100 Days of SwiftUI


Please, can anyone here explain why this predicate doesn't work:

NSPredicate(format: "isActive == %@, true)

if the next one works fine:

NsPredicate(format: "name BEGINSWITH %@, "A")

thanks for your reply


// Solution 1 [ NSNumber ]
let bool = NSNumber(booleanLiteral: true)
let predicate = NSPredicate(format: "isActive = %@", bool as CVarArg)

// or

NSPredicate(format: "isActive == %@", true as NSNumber)

// Solution 2 [ Bool ] (static example)
let predicate = NSPredicate(format: "isActive == YES")

%@ isn't a valid substitution for a Bool parameter. You should be using true as NSNumber instead. It's needed for interacting with C as far as I understood.



Thanks for your help


The first predicate you mentioned, NSPredicate(format: "isActive == %@, true"), is not working because you have an extra comma after the %@ placeholder. The correct syntax for this predicate would be:

NSPredicate(format: "isActive == %@", true)

The second predicate, NSPredicate(format: "name BEGINSWITH %@, "A"), is working fine because it is using the correct syntax for the BEGINSWITH operator. The %@ placeholder is used to indicate where the dynamic value will be inserted into the predicate format string.

Note that you should ensure that the property you are trying to filter on (isActive or name in your examples) exists and is spelled correctly, and that it is a property of the object you are filtering.


The issue with the first predicate is a syntax error in the format string. The correct format for checking equality in NSPredicate is to use "%@ == %@", where the first "%@" represents the attribute you're comparing and the second "%@" represents the value you're comparing against.

Corrected predicate for "isActive" attribute:

NSPredicate(format: "isActive == %@", true)

On the other hand, the second predicate is correctly formatted. The "BEGINSWITH" operator compares the beginning of a string attribute ("name") with a specified value ("A"). So, the predicate "name BEGINSWITH %@", "A" will match all objects where the "name" attribute starts with the letter "A".

Corrected predicate:

NSPredicate(format: "name BEGINSWITH %@", "A")

[Remember to ensure]) proper syntax and matching placeholders (%@) when using NSPredicate to accurately define your search criteria.


Hacking with Swift is sponsored by Judo

SPONSORED Let’s face it, SwiftUI previews are limited, slow, and painful. Judo takes a different approach to building visually—think Interface Builder for SwiftUI. Build your interface in a completely visual canvas, then drag and drop into your Xcode project and wire up button clicks to custom code. Download the Mac App and start your free trial today!

Try now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.