51

Here is my current NSPredicate:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"UPC==%@ OR ItemID==%@", aUPCCode,aUPCCode];

How can I make this case insensitive?

And I do not want to do any partial matching.

Example if they enter 123 for aUPCCode I do not want to get 123, 123a, 123b, 123c, ect. I would only want an exact match.

I thought about doing this but it seems a little ridiculous:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"UPC==%@ OR ItemID==%@ OR UPC==%@ OR ItemID==%@ OR UPC==%@ OR ItemID==%@", aUPCCode,aUPCCode,[ aUPCCode lowercaseString] ,[aUPCCode lowercaseString], [aUPCCode uppercaseString],[aUPCCode uppercaseString]];
Slee
  • 27,498
  • 52
  • 145
  • 243
  • 1
    Clarification: You say, " if they enter 123 for aUPCCode I do not want to get 123..." That doesn't make any sense. Is that a typo. Also, you seem to be implying that your current predict is "greedy" test that matches any value that starts with `123`. Is that the case? – TechZen Sep 25 '11 at 15:57
  • What type of persistent store are you using? – TechZen Sep 25 '11 at 15:57

3 Answers3

100

As Dave DeLong said, you can use:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"UPC ==[c] %@ OR ItemID ==[c] %@", aUPCCode,aUPCCode];

Edit:

Use ==[c] instead of ==[cd] or you get accents too (abcd == àbcd).

Sinetris
  • 8,288
  • 2
  • 22
  • 17
18

As Sinetris said, the above works in Objective-C.

It works for case-insensitive. Fetches all results which have the "example" string value in it.

Update Swift 4.0/5.0

let predicateIsNumber = NSPredicate(format: "keywordContactNo contains[c] %@", example!) 

Hope it helps

Thanks

Pranav Kasetti
  • 8,770
  • 2
  • 50
  • 71
Harjot Singh
  • 6,767
  • 2
  • 38
  • 34
0

maybe this:

[NSPredicate predicateWithFormat:@"UPC MATCHES[cd] %@ OR ItemID MATCHES[cd] %@",aUPCCode,aUPCCode];
Michael Frederick
  • 16,664
  • 3
  • 43
  • 58