0

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,

4
  • A placeholder for a Bool is %d or %ld rather than %@ Commented May 15, 2017 at 6:35
  • @vadian, thanks for your help but still it is not working! Commented May 15, 2017 at 6:39
  • Try this : let predicate = NSPredicate(format: "rec_name LIKE[cd] %@ AND rec_name LIKE[cd] %@ AND isActive = %@","apple","macbook",NSNumber(booleanLiteral: true)) Commented May 15, 2017 at 6:40
  • try this fetchRequest.predicate = NSPredicate(format: "rec_name LIKE[cd] %@ AND rec_name LIKE[cd] %@ AND isActive = %d","apple","macbook",true) Commented May 15, 2017 at 6:41

4 Answers 4

1

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)
Sign up to request clarification or add additional context in comments.

2 Comments

Better: NSNumber(value: true) or true as NSNumber, compare stackoverflow.com/a/42933246/1187415. There is usually no reason to call the "xxxLiteral" initializers, they are required only for the compiler.
@MartinR Thanks for the suggestion, edited answer with it
0

Try this

fetchRequest.predicate =  NSPredicate(format: "rec_name LIKE[cd] %@ AND rec_name LIKE[cd] %@ AND isActive = %@","apple","macbook",NSNumber(booleanLiteral: true))

Comments

0

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.

Comments

0

Try this

NSPredicate(format: "rec_name == %@ AND rec_name == %@ AND isActive = %d","apple","macbook",true)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.