Reputation: 651
Need
I have to find the record with rec_name is "Apple 256GB Macbook" & isActive == true.
Fetch Request
fetchRequest.predicate = NSPredicate(format: "rec_name LIKE[cd] *%@* AND rec_name LIKE[cd] *%@* AND isActive = %@","apple","macbook",true)
Error
Unable to parse the format string "rec_name LIKE[cd] *%@* AND rec_name LIKE[cd] *%@* AND isActive = %@"
Thank you,
Upvotes: 0
Views: 526
Reputation: 72410
The problem is because of *%@*
here %@
between the *
is not replaced by a string argument so NSPredicate
is not able to make your query LIKE *apple*
and LIKE *macbook*
what you need to do is make a search string with wildcards and with Like simply use %@
, also as @Vadian suggested use number instead of Bool
.
let searchkeyword1 = "apple"
let searchkeyword2 = "macbook"
//Make search string wildcards charaters
let searchString1 = "*\(searchkeyword1)*"
let searchString2 = "*\(searchkeyword2)*"
fetchRequest.predicate = NSPredicate(format: "rec_name LIKE[cd] %@ AND rec_name LIKE[cd] %@ AND isActive = %@", searchString1,searchString2, true as NSNumber)
Also batter option here is you can use CONTAINS
and no need to use of wildcards.
fetchRequest.predicate = NSPredicate(format: "rec_name CONTAINS[cd] %@ AND rec_name CONTAINS[cd] %@ AND isActive = %@", "apple","macbook", true as NSNumber)
You can do one more thing if you want to find all record start with apple and end with macbook then you can simply use BEGINSWITH
and ENDSWITH
fetchRequest.predicate = NSPredicate(format: "rec_name BEGINSWITH[cd] %@ AND rec_name ENDSWITH[cd] %@ AND isActive = %@", "apple","macbook", true as NSNumber)
Upvotes: 1
Reputation: 3310
Try this
NSPredicate(format: "rec_name == %@ AND rec_name == %@ AND isActive = %d","apple","macbook",true)
Upvotes: 0
Reputation: 49710
You need to set a predicate query like following:
fetchRequest.predicate = NSPredicate(format: "rec_name == %@ AND isActive == %@" , "Apple 256GB Macbook", NSNumber(booleanLiteral: true))
I don't think we need to use Like if you are looking for perfect match.
Upvotes: 0
Reputation: 8322
Try this
fetchRequest.predicate = NSPredicate(format: "rec_name LIKE[cd] %@ AND rec_name LIKE[cd] %@ AND isActive = %@","apple","macbook",NSNumber(booleanLiteral: true))
Upvotes: 0