Search code examples
iphoneobjective-ccore-datauisearchbaruisearchdisplaycontroller

Grouping in search display controller


In my Core Data app, I have an entity Person with names as attribute. I used following predicate logic to search the names. It displays names when we search with a word. well but I want two groups of searched names as

  1. Word containing anywhere in the name

  2. Name starts withe the word.

both groups I want. How?

NSString *searchfilter = [NSString stringWithFormat:@"*%@*",savedSearchTerm_];
NSPredicate *filter = [NSPredicate predicateWithFormat:@"word like[c] %@", searchfilter];
[fetchRequest setPredicate:filter];

Solution

  • Your example should work for #1.

    word is anywhere anywhere in the name:

    NSString *searchfilter = [NSString stringWithFormat:@"*%@*",savedSearchTerm_];
    NSPredicate *filter = [NSPredicate predicateWithFormat:@"word like[c] %@", searchfilter];
    [fetchRequest setPredicate:filter];
    

    name starts with the word:

    NSString *searchfilter = [NSString stringWithFormat:@"%@*",savedSearchTerm_];
    NSPredicate *filter = [NSPredicate predicateWithFormat:@"word like[c] %@", searchfilter];
    [fetchRequest setPredicate:filter];