我有以下方法:
- (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];
原文地址:https://www.jb51.cc/iOS/336502.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。