使用NSPredicate过滤可变阵列

时间:2016-06-02 01:19:38

标签: objective-c nsmutablearray nspredicate

我有一个MutableArray,每个子阵列中有6个子数组和许多子数组Click here for screenshot 每个阵列都包含电台的细节......(有146个阵列
共)

我的目标是使用" Name"过滤数组,但同时我也要提取剩余的详细信息。我尝试使用谓词方法,但它失败了。

我的searchResults数组应该具有过滤的数组,其中包含基于"名称"的站点的详细信息。有人可以帮我实现这个目标吗?提前谢谢。

-(void)filterContentForSearchText:(NSString *)searchText{


NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"ANY Name CONTAINS[cd] %@",searchText];
_searchResults = [ _stationsArray filteredArrayUsingPredicate:resultPredicate];

}


- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(nullable NSString *)searchString{

[self filterContentForSearchText:searchString];
return YES;
}

2 个答案:

答案 0 :(得分:0)

你可以试试!!!

    #import <Foundation/Foundation.h>

    @interface StationDetail : NSObject

        @property (nonatomic, strong) NSString *name;

    @end

    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.

        // Initialize 
        stationsArray = [[NSMutableArray alloc] init];

        NSArray *stationName = [[NSArray alloc] initWithObjects: @"Chennai", @"Bangalore", @"Kochi", @"Mumbai", @"Delhi", @"Hyderbad", @"Pune", @"Agra", @"Jaipur", nil];

        int index = 0;
        for (int i = 0; i < 3; i++) {
            NSMutableArray *substationsArray = [[NSMutableArray alloc] init];
             for (int j = 0; j < 3; j++) {
                 StationDetail *station = [[StationDetail alloc] init];
                 [station setName:[stationName objectAtIndex:index]];
                 [substationsArray addObject:station];
                 index++;

             }
            [stationsArray addObject:substationsArray];
        }


    }


    -(void)filterContentForSearchText:(NSString *)searchText{
        // only one parameter of the object need to be filtered
        NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"name CONTAINS[cd] %@",searchText]; // name is object key name 
        NSArray *searchResults = [ stationsArray filteredArrayUsingPredicate:resultPredicate];
        NSLog(@"%lu",(unsigned long)[searchResults count]);

    }

    -(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
        [self filterContentForSearchText:searchBar.text];
    }

答案 1 :(得分:0)

得到了解决方案。

-(void)filterContentForSearchText:(NSString *)searchText{

 allResults = [[NSMutableArray alloc]init];

NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"Name CONTAINS[cd] %@",searchText];


 //used for loop to check all sub-arrays.

for (int i=0; i<6; i++) {
     _searchResults = [ _stationsArray[i]   filteredArrayUsingPredicate:resultPredicate];
    [allResults addObjectsFromArray:_searchResults];
}


}

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(nullable NSString *)searchString{




[self filterContentForSearchText:searchString];
return YES;
}