[英]How to filter a NSArray by Key:value, that is in a NSDictionary
[英]filter NSArray with NSDictionary by int value
我有以下NSArray:
(
{
establecimiento = 15;
internet = 500;
},
{
establecimiento = 0;
internet = 1024;
},
{
establecimiento = 24;
internet = 300;
}
)
我需要过滤establecimiento < 10
的数组。
我正在尝试:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"establecimiento = %@", [NSNumber numberWithInt:self.establecimientoFiltro]];
其中self.establecimientoFiltro
是一个整数,其值是:10
但结果我有一个空数组。
希望清楚我的问题,并预先感谢您的回答。
问候,维克多
您的谓词检查以查看该值是否等于十,不小于十。 您收到一个空数组,因为您提供给我们的数组不包含establecimiento
键返回值10的字典。
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"establecimiento < 10"];
您应该将blocks
与Predicate
结合使用以实现此目的。
假设您的数组在myArray
。 现在根据您的选择范围做出谓词。 截至目前,您希望establecimientoFiltro
值小于10。
NSPredicate *keyPred = [NSPredicate predicateWithBlock:^BOOL(id obj, NSDictionary *bindings) {
NSRange myRange = NSMakeRange (0, 9);
NSInteger rangeKey = [[obj valueForKey:@"establecimientoFiltro"]integerValue];
if (NSLocationInRange(rangeKey, myRange)) {
// found
return YES;
} else {
// not found
return NO;
}
}];
NSArray *filterArray = [myArray filteredArrayUsingPredicate:keyPred];
NSLog(@"%@", filterArray);
您将结果过滤在filteredArray
。
希望这对您最有效和最有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.