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.
前2个谓词工作正常。但最后一个(aToZPredicate),是行不通的。 stationSearchData是一个StationSearchData对象,而browseAtozArray是一个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:
这是StationSearchData对象的接口:
@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!
谢谢!
1 个解决方案
#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
首先,您不应该使用stringWithFormat来构建谓词。如果搜索文本包含任何特殊字符(如“或”),这可能会导致问题。所以您应该替换
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:
要在数组中搜索,您必须在谓词中使用“ANY”:
NSPredicate *aToZPredicate =
[NSPredicate predicateWithFormat:@"ANY stationSearchData.browseAtozArray.city CONTAINS[c] %@", text];
#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
首先,您不应该使用stringWithFormat来构建谓词。如果搜索文本包含任何特殊字符(如“或”),这可能会导致问题。所以您应该替换
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:
要在数组中搜索,您必须在谓词中使用“ANY”:
NSPredicate *aToZPredicate =
[NSPredicate predicateWithFormat:@"ANY stationSearchData.browseAtozArray.city CONTAINS[c] %@", text];