2

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?

1
  • hi @SuperKevin did you found a solution for this question ? Commented Jan 22, 2016 at 17:41

1 Answer 1

2

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");
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, but these arrays are in the parse system. I am asking how I can pull these matching values from parse, I do not have these arrays in memory.
Sorry, I don't know. Not familiar with Parse.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.