
- (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;} 前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
谢谢!
解决方法 首先,你不应该使用stringWithFormat来构建谓词.这可能导致问题如果搜索文本包含任何特殊字符,如“或”.
所以你应该更换
Nsstring *nameformatString = [Nsstring stringWithFormat:@"stationname contains[c] '%@'",text];nspredicate *namePredicate = [nspredicate predicateWithFormat:nameformatString];
通过
nspredicate *namePredicate = [nspredicate predicateWithFormat:@"stationname contains[c] %@",text];
要在数组中搜索,您必须在谓词中使用“ANY”:
nspredicate *aToZPredicate = [nspredicate predicateWithFormat:@"ANY stationSearchData.browseAtozArray.city CONTAINS[c] %@",text];总结
以上是内存溢出为你收集整理的ios – 数组内的对象内的NSPredicate过滤器数组全部内容,希望文章能够帮你解决ios – 数组内的对象内的NSPredicate过滤器数组所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)