14

I have the following method:

- (NSMutableArray *)getFilteredArrayFromArray:(NSMutableArray *)array withText:(NSString *)text {

if ([array count] <= 0)
    return nil;

NSMutableArray *arrayToFilter = [NSMutableArray arrayWithArray:array];
NSString *nameformatString = [NSString stringWithFormat:@"stationName contains[c] '%@'", text];
NSPredicate *namePredicate = [NSPredicate predicateWithFormat:nameformatString];

NSString *descformatString = [NSString stringWithFormat:@"stationTagline contains[c] '%@'", text];
NSPredicate *descPredicate = [NSPredicate predicateWithFormat:descformatString];

NSString *aToZformatString = [NSString stringWithFormat:@"stationSearchData.browseAtozArray.city contains[c] '%@'", text];
NSPredicate *aToZPredicate = [NSPredicate predicateWithFormat:aToZformatString];

NSPredicate * combinePredicate = [NSCompoundPredicate orPredicateWithSubpredicates:[NSArray arrayWithObjects:namePredicate, descPredicate, aToZPredicate, nil]];

[arrayToFilter filterUsingPredicate:combinePredicate];

return arrayToFilter;
}

The first 2 predicates work fine. But the last one (aToZPredicate), is not working. stationSearchData is a StationSearchData object, and browseAtozArray is an NSMutableArray.

How can I use a predicate to essentially search an array within an array within an array?

Here is the interface for the StationSearchData object:

@interface StationSearchData : NSObject

@property (nonatomic, strong) NSString *location;
@property (nonatomic, strong) NSString *city;
@property (nonatomic, strong) NSString *latitude;
@property (nonatomic, strong) NSString *longitude;

@property (nonatomic, strong) NSMutableArray *browseAtozArray;
@property (nonatomic, strong) NSMutableArray *genreArray;

@end

Thanks!

6
  • You are using "or predicate". Try with "and predicate" which is andPredicateWithSubpredicates: Commented Jan 13, 2014 at 15:39
  • Thanks for the reply but that doesnt make a difference. Plus I want to use an OR type search for this. Commented Jan 13, 2014 at 15:43
  • try add self to your predicate like: self.stationSearchData.browseAtozArray.city Commented Jan 13, 2014 at 15:50
  • Just tried and didn't work. Commented Jan 13, 2014 at 15:53
  • @codeman: Is stationSearchData now an array or not? Commented Jan 13, 2014 at 16:02

1 Answer 1

29

First of all, you should not use stringWithFormat to build predicates. This can cause problems if the search text contains any special characters like ' or ". So you should replace

NSString *nameformatString = [NSString stringWithFormat:@"stationName contains[c] '%@'", text];
NSPredicate *namePredicate = [NSPredicate predicateWithFormat:nameformatString];

by

NSPredicate *namePredicate = [NSPredicate predicateWithFormat:@"stationName contains[c] %@", text];

To search within an array, you have to use "ANY" in the predicate:

NSPredicate *aToZPredicate =
  [NSPredicate predicateWithFormat:@"ANY stationSearchData.browseAtozArray.city CONTAINS[c] %@", text];
Sign up to request clarification or add additional context in comments.

Comments

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.