问题
I need to search and filter only in array's that set ON in settings.
Now I loop in dict
for all array's:
- (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
I have a NSMutableArray
named resultArray
which collect my array's that was set ON in settings
NSLog(@"%@", resultArray);
2013-05-01 11:40:46.917 testApp[5378:907] (
Green,
Orange,
Blue
)
How to search only in resultArray
? I tried:
NSArray *array = [dict objectForKey:resultArray];
but without success.
This work only for one array:
NSArray *array = [dict objectForKey:@"Blue"];
My data structure is:

回答1:
Try
- (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];
}
}
}
}
来源:https://stackoverflow.com/questions/16315276/search-only-in-arrays-that-set-on-in-settings