我有以下方法:
- (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;
}
Run Code Online (Sandbox Code Playgroud)
前2个谓词工作正常.但最后一个(aToZPredicate),是行不通的.stationSearchData是一个StationSearchData对象,而browseAtozArray是一个NSMutableArray.
如何使用谓词基本上搜索数组中数组中的数组?
这是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
Run Code Online (Sandbox Code Playgroud)
谢谢!
Mar*_*n R 29
首先,您不应该使用stringWithFormat构建谓词.如果搜索文本包含任何特殊字符(如'或),则可能会出现问题".所以你应该更换
NSString *nameformatString = [NSString stringWithFormat:@"stationName contains[c] '%@'", text];
NSPredicate *namePredicate = [NSPredicate predicateWithFormat:nameformatString];
Run Code Online (Sandbox Code Playgroud)
通过
NSPredicate *namePredicate = [NSPredicate predicateWithFormat:@"stationName contains[c] %@", text];
Run Code Online (Sandbox Code Playgroud)
要在数组中搜索,您必须在谓词中使用"ANY":
NSPredicate *aToZPredicate =
[NSPredicate predicateWithFormat:@"ANY stationSearchData.browseAtozArray.city CONTAINS[c] %@", text];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16870 次 |
| 最近记录: |