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,

nirav
  • 573
  • 5
  • 20
  • A placeholder for a `Bool` is `%d` or `%ld` rather than `%@` – vadian May 15 '17 at 06:35
  • @vadian, thanks for your help but still it is not working! – nirav May 15 '17 at 06:39
  • Try this : let predicate = NSPredicate(format: "rec_name LIKE[cd] *%@* AND rec_name LIKE[cd] *%@* AND isActive = %@","apple","macbook",NSNumber(booleanLiteral: true)) – KKRocks May 15 '17 at 06:40
  • try this fetchRequest.predicate = NSPredicate(format: "rec_name LIKE[cd] %@ AND rec_name LIKE[cd] %@ AND isActive = %d","apple","macbook",true) – jignesh Vadadoriya May 15 '17 at 06:41

4 Answers4

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)
Nirav D
  • 71,513
  • 12
  • 161
  • 183
  • 1
    Better: `NSNumber(value: true)` or `true as NSNumber`, compare http://stackoverflow.com/a/42933246/1187415. There is usually no reason to call the "xxxLiteral" initializers, they are required only for the compiler. – Martin R May 15 '17 at 07:46
  • 1
    @MartinR Thanks for the suggestion, edited answer with it – Nirav D May 15 '17 at 07:55
0

Try this

fetchRequest.predicate =  NSPredicate(format: "rec_name LIKE[cd] %@ AND rec_name LIKE[cd] %@ AND isActive = %@","apple","macbook",NSNumber(booleanLiteral: true))
KKRocks
  • 8,222
  • 1
  • 18
  • 84
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.

Nitin Gohel
  • 49,482
  • 17
  • 105
  • 144
0

Try this

NSPredicate(format: "rec_name == %@ AND rec_name == %@ AND isActive = %d","apple","macbook",true)
jignesh Vadadoriya
  • 3,244
  • 3
  • 18
  • 29