NSPredicate contains string lowercase

VansFannel picture VansFannel · Jan 30, 2013 · Viewed 33.3k times · Source

I'm developing an iOS application with iOS SDK 6.0 and XCode 4.5.2. My target development is 4.3.

I'm using Core Data to manage my data. Now I have this NSPredicate to search shops:

if ((shopSearchBar.text != nil) && ([shopSearchBar.text length] > 0))
{
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name = %@",shopSearchBar.text];
    [fetchRequest setPredicate:predicate];
}

This is Shop entity:

enter image description here

I have to convert name to lowercase and see if it contains shopSearchBar.text in lowercase format.

Example:

I have these four shops:

  • Shop1
  • shop 1
  • my shop
  • Shop

If user search text is 'shop', it must returns all of them.

Do you know how to do that?

Answer

VansFannel picture VansFannel · Jan 30, 2013

This is how I've solved my problem:

if ((shopSearchBar.text != nil) && ([shopSearchBar.text length] > 0))
{
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name CONTAINS[cd] %@",
                              shopSearchBar.text];
    [fetchRequest setPredicate:predicate];
}