Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Searching on the properties of objects in an array

I have an NSArray of objects with properties such as firstName, lastName, clientID etc. and i would like to perform a search on the array based on a search keyword. This keyword must be checked against the first name and the last name properties and return a subset of the original array that contains only those objects whose first/last name contain the search term. Is there any efficient/fast way to do this?

like image 692
John Baum Avatar asked Jan 30 '26 08:01

John Baum


1 Answers

I think your looking for -indexesOfObjectsPassingTest:

NSIndexSet *indexSet = [array indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
    MyObject *myObject = (MyObject *)obj;
    return [myObject.firstName isEqualToString:@"Bob"];
}];

This returns an index set of all the objects in the array with the first name of "Bob".

like image 130
Jeffery Thomas Avatar answered Feb 01 '26 23:02

Jeffery Thomas