LAST CHANCE: Save 50% on all my Swift books and bundles! >>

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      

Hacking with Swift is sponsored by Essential Developer.

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until July 28th.

Click to save your free spot now

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.