如何实现在字典数组中searchstring?

我有一个应用程序,我有一个nsmutablearrays创build像这样

NSDictionary *memberInfo = [self.currentChannel infoForMemberWithID:memberID]; for(int i=0;i<self.currentChannel.memberCount;i++) { [searchfriendarray addObject:memberInfo]; } NSLog(@"dfdfdfsear%@",searchfriendarray); 

我得到的回应是这个

 dfdfdfsear( { name = baddd; status = "<null>"; }, { name = baddd; status = "<null>"; } ) 

`现在我想在这个方法中search一个nsstring

 - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText 

任何人都可以帮我实现search和加载我的tableview根据?

使用NSPredicate来过滤数组。

 NSPredicate *filterPredicate = [NSPredicate predicateWithFormat:@"name contains[cd] %@", searchString]; [yourMutableArray filterUsingPredicate:filterPredicate]; 

您可以使用NSPredicate

谓词
谓词是过滤数组的有用对象。 这有点像在SQL中使用简单的where子句进行select。

如果你有这种格式的数组

 NSMutableArray *yourAry; ( { category = classic; quote = "Frankly, my dear, I don't give a damn."; source = "Gone With the Wind"; }, { category = classic; quote = "Here's looking at you, kid."; source = Casablanca; } { category = modern; quote = "I solemnly swear that I am up to no good."; source = "Harry Potter and the Prisoner of Azkaban"; }, { category = modern; quote = "You like pain? Try wearing a corset."; source = "Pirates of the Caribbean"; } ) 

而你想要search所有的数组category = classic那么我们可以使用NSPredicate 。

 NSString *selectedCategory = @"classic"; //filter array by category using predicate NSPredicate *predicate = [NSPredicate predicateWithFormat:@"category == %@", selectedCategory]; NSArray *filteredArray = [self.movieQuotes filteredArrayUsingPredicate:predicate]; 

如果您需要任何帮助MyBlog,请关注此博客