【问题标题】:Search only in array's that set ON in settings仅在设置中设置为 ON 的数组中搜索
【发布时间】:2013-04-25 06:45:15
【问题描述】:

我只需要在设置中设置为 ON 的数组中进行搜索和过滤。

现在我在 dict 中循环所有数组:

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope 
{
    NSPredicate *resultPredicate = [NSPredicate
                                    predicateWithFormat:@"Name CONTAINS[cd] %@ OR Address CONTAINS[cd] %@",
                                    searchText,
                                    searchText];

    searchResults = [NSMutableArray array];

    for (NSString *akey in dict) {

        NSLog(@"%@", akey);

        NSArray *array = [dict objectForKey:akey];

        NSArray *matches = [array filteredArrayUsingPredicate:resultPredicate];

        if (matches.count > 0) {
            [searchResults addObjectsFromArray:matches];
        }
    }
}

NSLog(@"%@", akey);

2013-05-01 11:34:07.256 testApp[5357:907] White
2013-05-01 11:34:07.262 testApp[5357:907] Yellow
2013-05-01 11:34:07.274 testApp[5357:907] Greenyellow
2013-05-01 11:34:07.275 testApp[5357:907] Darkblue
2013-05-01 11:34:07.277 testApp[5357:907] Green
2013-05-01 11:34:07.283 testApp[5357:907] Purple
2013-05-01 11:34:07.284 testApp[5357:907] Blue
2013-05-01 11:34:07.289 testApp[5357:907] Black
2013-05-01 11:34:07.291 testApp[5357:907] Red
2013-05-01 11:34:07.294 testApp[5357:907] Orange
2013-05-01 11:34:07.299 testApp[5357:907] BlueYellow
2013-05-01 11:34:07.305 testApp[5357:907] Brown
2013-05-01 11:34:07.308 testApp[5357:907] Rose
2013-05-01 11:34:07.312 testApp[5357:907] Gray

我有一个名为 resultArrayNSMutableArray,它收集我在设置中设置为 ON 的数组

NSLog(@"%@", resultArray);

2013-05-01 11:40:46.917 testApp[5378:907] (
    Green,
    Orange,
    Blue
)

如何只搜索resultArray?我试过了:

NSArray *array = [dict objectForKey:resultArray];

但没有成功。

这仅适用于一个数组:

NSArray *array = [dict objectForKey:@"Blue"];

我的数据结构是:

【问题讨论】:

  • 这个完全令人困惑。
  • 现在我已经设置了绿色、橙色和蓝色数组,我只需要在那里搜索,而不是全部搜索。

标签: ios search filter nsarray nspredicate


【解决方案1】:

试试

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope 
{
    NSPredicate *resultPredicate = [NSPredicate
                                    predicateWithFormat:@"Name CONTAINS[cd] %@ OR Address CONTAINS[cd] %@",
                                    searchText,
                                    searchText];

    searchResults = [NSMutableArray array];

    for (NSString *akey in dict) { 

        if ([resultsArray containsObject:aKey]) {

            NSArray *array = dict[akey];

            NSArray *matches = [array filteredArrayUsingPredicate:resultPredicate];

            if (matches.count > 0) {
                [searchResults addObjectsFromArray:matches];
            }

        }        

    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-06
    • 1970-01-01
    • 2011-08-15
    • 2013-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-02
    相关资源
    最近更新 更多