数组内的对象中的NSPredicatefilter数组

我有以下的方法:

- (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; } 

前两个谓词工作正常。 但最后一个(aToZPredicate),不起作用。 stationSearchData是一个StationSearchData对象,browseAtozArray是一个NSMutableArray。

我怎样才能使用谓词本质上searcharrays内的数组内的数组?

这是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来构build谓词。 如果search文本包含任何特殊字符(如'"则会导致问题,因此您应该replace

 NSString *nameformatString = [NSString stringWithFormat:@"stationName contains[c] '%@'", text]; NSPredicate *namePredicate = [NSPredicate predicateWithFormat:nameformatString]; 

通过

 NSPredicate *namePredicate = [NSPredicate predicateWithFormat:@"stationName contains[c] %@", text]; 

要在数组中search,必须在谓词中使用“ANY”:

 NSPredicate *aToZPredicate = [NSPredicate predicateWithFormat:@"ANY stationSearchData.browseAtozArray.city CONTAINS[c] %@", text];