2 mapmapm MapMapM 于 2013.01.04 15:18 提问

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中实现循环数组。

不知道应该怎么解决,请指教。

2个回答

gaoXxxing
gaoXxxing   2013.01.04 16:30
已采纳
NSString* text = @"mac" ;
NSPredicate* predicate = [NSPredicate predicateWithFormat:@"any data contains[cd] %@",text] ;
NSArray* filteredArray = [theArray filteredArrayUsingPredicate:predicate] ;
LPPloveROU
LPPloveROU   2013.01.04 16:28

我说可以用块来过滤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];
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!