Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search through NSArray for string

Tags:

I would like to search through my NSArray for a certain string.

Example:

NSArray has the objects: "dog", "cat", "fat dog", "thing", "another thing", "heck here's another thing"

I want to search for the word "another" and put the results into one array, and have the other, non results, into another array that can be filtered further.

like image 932
Matt S. Avatar asked Dec 05 '09 23:12

Matt S.


1 Answers

If the strings inside the array are known to be distinct, you can use sets. NSSet is faster then NSArray on large inputs:

NSArray * inputArray = [NSMutableArray arrayWithObjects:@"one", @"two", @"one again", nil];

NSMutableSet * matches = [NSMutableSet setWithArray:inputArray];
[matches filterUsingPredicate:[NSPredicate predicateWithFormat:@"SELF contains[c] 'one'"]];

NSMutableSet * notmatches = [NSMutableSet setWithArray:inputArray];
[notmatches  minusSet:matches];
like image 94
diciu Avatar answered Nov 03 '22 00:11

diciu