ios – 数组内的对象内的NSPredicate过滤器数组

我有以下方法
- (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];

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...