user1904273
user1904273

Reputation: 4764

Filter NSArray of Arrays by One Element of Array Using NSPredicate in IOS

It is possible to filter an array of strings as follows:

NSArray *array =  @[@"honda",@"toyota",@"ford"];
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF contains[cd] %@",@"ford"];
NSArray *filtered = [array filteredArrayUsingPredicate:pred];

I want to search an array that contains arrays of two strings by the values for the first of the strings. So for:

NSArray *cars = @[@[@"honda",@"accord"],@[@"toyota",@"corolla"],@[@"ford",@"explorer"]];

I want to search the first dimension (honda, toyota, ford) for @"ford"

Is there a way to tell the predicate I want to search on only the first attribute and return matching elements of the array?

Upvotes: 0

Views: 41

Answers (1)

E.Coms
E.Coms

Reputation: 11531

Well here is the pred you need.

     NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF[FIRST] contains[cd] %@", @"ford"];

Upvotes: 1

Related Questions