Skip to content

Instantly share code, notes, and snippets.

@chrisladd
Created March 19, 2014 16:48
Show Gist options
  • Save chrisladd/9645970 to your computer and use it in GitHub Desktop.
Save chrisladd/9645970 to your computer and use it in GitHub Desktop.
Some very old code implementing basic search split on words. Don't judge.
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
NSMutableSet *uniqueCharts = [NSMutableSet set];
NSMutableCharacterSet *charSet = [[NSMutableCharacterSet alloc] init];
[charSet formUnionWithCharacterSet:[NSCharacterSet punctuationCharacterSet]];
[charSet formUnionWithCharacterSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSArray *words = [searchText componentsSeparatedByCharactersInSet:charSet];
BOOL firstLoop = YES;
for (NSString *aWord in words) {
if (aWord.length < 3) {
break;
}
NSPredicate *aPredicate = [NSPredicate prefixMatchingPredicateForString:aWord withKeyPath:@"word"];
if (!aPredicate) {
break;
}
NSArray *results = [self fetchChartsWithPredicate:aPredicate]; // returns a unique array of TncManagedCharts
// on the first run, populate with the results. On subsequent runs, intersect the two
if (firstLoop) {
uniqueCharts = [NSMutableSet setWithArray:results];
firstLoop = NO;
} else {
[uniqueCharts intersectSet:[NSSet setWithArray:results]];
}
if (IsEmpty(uniqueCharts)) {
// if we have no results, we won't have any on subsequent loops either. So no need to search further
self.searchArray = nil;
return;
}
}
[self resetSearchArrayFromUniqueCharts:uniqueCharts];
}
@chrisladd
Copy link
Author

I think I actually meant continue above where I said break

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment