问题描述
- NSDictionary中NSArray的NSPredicate
-
数组:( {id:1,data:(@"macbook",@"mac mini")}, {id:2,data:(@"ipad",@"ipod")}, {id:3,data:(@"macbook",@"ipod")} )
predicate:
NSString *text = @"mac"; [NSPredicate predicateWithFormat:@"(data contains[cd] %@)",text]; [array filteredArrayUsingPredicate:predicate];
但是没有再dictionary中实现循环数组。
不知道应该怎么解决,请指教。
解决方案
NSString* text = @"mac" ;
NSPredicate* predicate = [NSPredicate predicateWithFormat:@"any data contains[cd] %@",text] ;
NSArray* filteredArray = [theArray filteredArrayUsingPredicate:predicate] ;
解决方案二:
我说可以用块来过滤NSArray
NSString * filterString = @"mac";
NSIndexSet * indexes = [array indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
NSArray * entry = obj[@"data"];
for (NSString * value in entry)
if ([value rangeOfString:filterString].location != NSNotFound)
return YES;
return NO;
}];
NSArray * filteredArray = [array objectsAtIndexes:indexes];
时间: 2024-11-08 21:55:37