NSPredicate filter array within object within array

后端 未结 1 1974
渐次进展
渐次进展 2020-12-14 10:33

I have the following method:

- (NSMutableArray *)getFilteredArrayFromArray:(NSMutableArray *)array withText:(NSString *)text {

if ([array count] <= 0)
           


        
相关标签:
1条回答
  • 2020-12-14 10:59

    First of all, you should not use stringWithFormat to build predicates. This can cause problems if the search text contains any special characters like ' or ". So you should replace

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

    by

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

    To search within an array, you have to use "ANY" in the predicate:

    NSPredicate *aToZPredicate =
      [NSPredicate predicateWithFormat:@"ANY stationSearchData.browseAtozArray.city CONTAINS[c] %@", text];
    
    0 讨论(0)
提交回复
热议问题