ios - 按日期过滤 hh :mm using NSPredicate

标签 ios filter nspredicate

我有一个包含多个字典的数组,其中有一个字段存储小时和分钟,如“14:22”。

如何按时间过滤数组?例如我想要所有字典,其中该字段的时间 > 01:00 和时间 < 18:00

最佳答案

像这样的东西会做你正在寻找的东西:

NSArray *data = @[@{@"time": @"10:05"},@{@"time": @"1:06"},@{@"time": @"18:24"},@{@"time": @"7:29"},@{@"time": @"11:55"}];
NSArray *filteredData = [data filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(NSDictionary *dict, NSDictionary *bindings) {
    NSArray *components = [dict[@"time"] componentsSeparatedByString:@":"];
    NSAssert(components.count >= 2, @"Invalid time format.");
    NSInteger h = [components[0] integerValue];
    NSInteger m = [components[1] integerValue];
    return h >= 1 && h < 18;
}]];

关于ios - 按日期过滤 hh :mm using NSPredicate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18799645/

相关文章:

iOS 模拟器 - 物理 Macbook 键盘不在 flutter 应用程序中提供输入

iOS Safari 使用错误的域作为相对 CSS 路径

Python pandas - 在 groupby 之后过滤行

Python:满足条件的列中的求和值

ios - 在保存到 coredata -Swift 之前检查数据是否存在

ios - Swift:Parse 中的长查询(OR/AND 条件)

ios - 使用低分辨率使用 AVCaptureVideoDataOutput 将 120/240 fps 捕获到帧缓冲区

使用列表作为条件的 Java 8 流过滤器

iphone - NSPredicate 格式等同于 SQL 查询

performance - 核心数据性能 : NSPredicate comparing objects