26

I am trying to write a NSPredicate to fetch rows with my_column value with this string "193e00a75148b4006a451452c618ccec" and I get the below crash.

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse the format string "my_column=193e00a75148b4006a451452c618ccec"'

My predicate statement

fetchRequest.predicate=[NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@==\"%@\"",attributeName,itemValue]];

also tried this

fetchRequest.predicate=[NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@ == %@",attributeName,itemValue]];

this

fetchRequest.predicate=[NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@ = %@",attributeName,itemValue]];

and this

fetchRequest.predicate=[NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@=\"%@\"",attributeName,itemValue]];

Please help.

I found out this, when I was trying with Martin R's answer

fetchRequest.predicate=[NSPredicate predicateWithFormat:@"%@==%@",attributeName,itemValue];

attributeName I pass comes with a ' ' so I took off attributeName and hardcoded it, then it works fine.

Satheesh
  • 10,998
  • 6
  • 50
  • 93

3 Answers3

52

Enclose the string in single quotes:

[NSPredicate predicateWithFormat:@"my_column = '193e00a75148b4006a451452c618ccec'"]

or better, use argument substitution:

[NSPredicate predicateWithFormat:@"my_column = %@", @"193e00a75148b4006a451452c618ccec"]

The second method avoids problems if the search string contains special characters such as ' or ".

Remark: Never use stringWithFormat when building predicates. stringWithFormat and predicateWithFormat handle the %K and %@ format differently, so combining these two methods is very error-prone (and unnecessary).

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
4

I think you have missed '' when checking for equality,

now try,

[NSPredicate predicateWithFormat:@"my_column='193e00a75148b4006a451452c618ccec'"];
Thilina Chamath Hewagama
  • 9,039
  • 3
  • 32
  • 45
3

Try

//Case Insensitive
fetchRequest.predicate=[NSPredicate predicateWithFormat:@"%K = [cd] %@",attributeName,itemValue];

//Case sensitive
fetchRequest.predicate=[NSPredicate predicateWithFormat:@"%K = %@",attributeName,itemValue];
Anupdas
  • 10,211
  • 2
  • 35
  • 60
  • 1) Note that "LIKE" does a wildcard comparison where "*" and "?" have a special meaning! - 2) For a fixed attribute name it is not necessary to use `%K`. – Martin R May 03 '13 at 12:17
  • @MartinR thanks for that info :). I just wanted to show that for an attribute the placeholder needs to be with %K. If using LIKE has got an issue, how can we do a case insensitive and diacritic search. – Anupdas May 03 '13 at 12:19
  • 1
    You can do `"my_column ==[cd] %@"` ! – Martin R May 03 '13 at 12:22
  • @MartinR Using %K for a fixed attribute name is necessary because the Core Data SQL generator otherwise has no way of knowing that the left side of the expression is a key path. It works without %K most of the time because Core Data tries to make educated guesses all the time. – Christian Kienle May 03 '13 at 14:35
  • @ChristianKienle Using %K would be more appropriate when a predicate is written for more generic use, here OP wanted to check for only a fixed attribute, that's what Martin's point was. I included it in my answer so that it would be useful for OP. Thanks for your comment. – Anupdas May 03 '13 at 14:43
  • @Anupdas IMHO you need %K always (at least in theory). The only reason why you don't need in in practice sometimes is because Core Data makes guesses. I know that because I wrote a NSIncrementalStore subclass which generates SQL from a NSPredicate and if you simply pass "attribute == value" as a predicate to Core Data then you have no way of knowing what "attribute" is. The left hand side of a expression can be anything. It does not always have to be a keypath. By using %K you actually get a keypath expression on the left hand side. Otherwise you get something else. – Christian Kienle May 03 '13 at 15:18
  • @ChristianKienle So are you saying that by including %K we are making the job easier for which ever function using predicate, since this avoids guesses ? – Anupdas May 03 '13 at 15:24
  • 1
    @Anupdas Don't get me wrong: This is a bit like splitting hair from my part. In 99% of the cases everything will be okay but yes: Using %K makes the job easier (and more robust) for everything/everyone that examines NSPredicates. You simply don't have to guess what the predicate might mean. – Christian Kienle May 03 '13 at 15:27
  • @ChristianKienle: I see your point. Most (all?) examples in the Apple documentation use inline attributes, and I have done so in my code, but it is correct that this can be ambiguous. – Martin R May 03 '13 at 21:01
  • In Swift 3 , i was able to solve similar error with the format as below: fetchRequest.predicate = NSPredicate(format: "title = \"\(bookInfo.title)\"", []) – Naishta Aug 07 '16 at 22:03