Reputation: 1916
I am using a parse database to store some records. I am storing an array of strings and I want to be able to search this array for substrings. Here is an example:
Array A:
["carbrown","blue","house","coldturkey"]
Array B:
["racecar","green","walking"]
Array C:
["greenturkey","users","published","ramp"]
I want to be able to search for a substring like car
and get Arrays A and B as matching results, Or searching for turkey
gives me matching results with arrays A and C, Or green
gives me Arrays B and C, and so on..
I know that for strings you can use this in parse:
- (void)whereKey:(NSString *)key containsString:(NSString *)substring
Is this possible with arrays, maybe something with regex?
Upvotes: 2
Views: 894
Reputation: 13766
You can use NSPredicate for this purpose. The following is an example.
NSArray *a = @[@"carbrown",@"blue",@"house",@"coldturkey"];
NSArray *b = @[@"racecar",@"green",@"walking"];
NSArray *c = @[@"greenturkey",@"users",@"published",@"ramp"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains[cd] %@", @"car"];
NSArray *filteredArrayA = [a filteredArrayUsingPredicate:predicate];
NSArray *filteredArrayB = [b filteredArrayUsingPredicate:predicate];
NSArray *filteredArrayC = [c filteredArrayUsingPredicate:predicate];
if ([filteredArrayA count]) {
NSLog(@"A has car in it");
}
Upvotes: 2