Search code examples
iosobjective-ccore-datanspredicate

Lowercase model object in iOS using predicate


NSPredicate *predicate = [NSPredicate predicateWithFormat:@"eventName In[c] %@", eventNameArray];

In this code I want the eventName keys to be of lowercase for matching, the above code is giving me eventNameArray objects as lowercased.

eventName is one of the object in my core data model, which I want to be lowercased.

NSArray *dbArray = [[NSArray alloc] initWithObjects:@"Add To Cart",@"Add To Cart",@"Lead Submitted",@"Add To Cart",@"Product Purchase", nil];
NSArray *eventArray = [[NSArray alloc] initWithObjects:@"add to cart",@"lead submitted", nil];
    
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self IN[c] %@", eventArray];
    
NSLog(@"%@",[dbArray filteredArrayUsingPredicate:predicate]);

This is working fine but not with model objects.


Solution

  • You can force the attribute into lowercase by using the lowercase:() function:

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"lowercase:(eventName) IN[c] %@", eventNameArray];