UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Day 61: predicate problem

Forums > 100 Days of SwiftUI

@Hery  

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

2      

// 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.

3      

@Hery  

Thanks for your help

2      

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.

3      

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.

3      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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.