如何在字典数组中实现字符串搜索?

时间:2012-10-17 05:48:48

标签: iphone ios ipad

我有一个应用程序,其中我有一个像这样创建的nsmutable数组.`

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>";
    }
)

` 现在我想用这个方法

搜索一个nsstring
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText

有人可以帮我实现搜索并根据它加载我的tableview吗?

2 个答案:

答案 0 :(得分:4)

使用NSPredicate过滤数组。

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

答案 1 :(得分:0)

您可以使用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";
    }
)  

并且您想搜索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

,请关注此博客