繁体   English   中英

通过int值过滤具有NSDictionary的NSArray

[英]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"];

您应该将blocksPredicate结合使用以实现此目的。

假设您的数组在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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM