-2

我有以下格式的字典数组。

myarary = {day = 0; 小时 = 1; 值 = 0;},{天 = 0; 小时 = 2; 值 = 0;}.... {第 6 天 =1; 小时=23;值 =1;}

所以基本上是 7 天,每天 24 小时,每小时值为 1 或 0。因此我的数组中共有 168 个字典。

现在我的任务是提取给定日期的几个小时范围内的值。例如,对于第 6 天,我必须提取 2、9 和 15 到 18 之间的另一个时间段,依此类推。

我设法解决了这个问题,使用以下格式的嵌套 for 循环

for (i =< start_of_hour_slot;i=<last_hour_of_slot); i++) for(j = 0; j<=6; j++)

现在这可行,但是它太长了,而且我的代码充满了循环,必须有更简单的快速枚举方法吗?

其次,我的 for 循环没有给我灵活性。

我喜欢在一个位置,我可以简单地提取让我们说第 7 天,三个不同的时间段,以及值。

或者可能持续多天,例如第 3、4、5 天、第 2-9、11、15 天...

4

2 回答 2

0

正如@vadian 建议的那样, NSCompoundPredicate 应该适用于您要完成的工作。看起来您在评论中发布的 NSPredicate 中可能有一些拼写错误,这就是它无法解析的原因。

#import "ViewController.h"

@interface ViewController ()
@property (strong, nullable) NSArray <NSDictionary *> *generatedArray;
- (NSArray <NSDictionary *> *)_generateFakeDictionaryArray;
- (NSPredicate *)_predicateForDays:(NSArray <NSNumber *> *)days andHours:(NSArray <NSNumber *> *)hours;
- (NSPredicate *)_predicateForDays:(NSArray <NSNumber *> *)days andHoursBetween:(NSArray <NSNumber *> *)hoursBetween;
- (NSPredicate *)_predicateForDaysBetween:(NSArray <NSNumber *> *)daysBetween andHours:(NSArray <NSNumber *> *)hours;
- (NSPredicate *)_predicateForDaysBetween:(NSArray <NSNumber *> *)daysBetween andHoursBetween:(NSArray <NSNumber *> *)hoursBetween;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.generatedArray = [self _generateFakeDictionaryArray];
}

- (void)viewDidAppear:(BOOL)animated {
    // one day multiple hour slots
    NSPredicate *specificDaysSpecificHours = [self _predicateForDays:@[@(6)] andHours:@[@(7), @(8), @(22)]];
    // multiple days hoursBetween
    NSPredicate *daysBetweenHoursBetween = [self _predicateForDaysBetween:@[@(3), @(5)] andHoursBetween:@[@(2), @(9)]];
    // days between, specific hours
    NSPredicate *daysBetweenSpecificHours = [self _predicateForDaysBetween:@[@(3), @(5)] andHours:@[@(11), @(15)]];
    NSCompoundPredicate *compPred = [NSCompoundPredicate orPredicateWithSubpredicates:@[specificDaysSpecificHours, daysBetweenHoursBetween, daysBetweenSpecificHours]];
    NSArray <NSDictionary *> *filteredArray = [self.generatedArray filteredArrayUsingPredicate:compPred];
    NSLog(@"Filtered array = %@", filteredArray);
}

- (NSArray <NSDictionary *> *)_generateFakeDictionaryArray {
    NSInteger daysInWeek = 7;
    NSInteger hoursInDay = 24;
    NSMutableArray *dictArray = [NSMutableArray arrayWithCapacity:hoursInDay * daysInWeek];
    for (NSInteger day = 0; day < daysInWeek; day++) {
        for (NSInteger hour = 0; hour < hoursInDay; hour++) {
            NSDictionary *dayHourDict = @{@"day" : @(day), @"hour" : @(hour), @"value" : @(arc4random() % 2)};
            [dictArray addObject:dayHourDict];
        }
    }
    return [NSArray arrayWithArray:dictArray];
}

- (NSPredicate *)_predicateForDays:(NSArray <NSNumber *> *)days andHours:(NSArray <NSNumber *> *)hours {
    return [NSPredicate predicateWithFormat:@"day IN %@ AND hour IN %@", days, hours];
}

- (NSPredicate *)_predicateForDays:(NSArray <NSNumber *> *)days andHoursBetween:(NSArray <NSNumber *> *)hoursBetween {
    return [NSPredicate predicateWithFormat:@"day IN %@ AND hour BETWEEN %@", days, hoursBetween];
}

- (NSPredicate *)_predicateForDaysBetween:(NSArray <NSNumber *> *)daysBetween andHours:(NSArray <NSNumber *> *)hours {
    return [NSPredicate predicateWithFormat:@"day BETWEEN %@ AND hour IN %@", daysBetween, hours];
}

- (NSPredicate *)_predicateForDaysBetween:(NSArray <NSNumber *> *)daysBetween andHoursBetween:(NSArray <NSNumber *> *)hoursBetween {
    return [NSPredicate predicateWithFormat:@"day BETWEEN %@ AND hour BETWEEN %@", daysBetween, hoursBetween];
}


@end

生成此作为输出:

Filtered array = (
        {
        day = 3;
        hour = 2;
        value = 1;
    },
        {
        day = 3;
        hour = 3;
        value = 0;
    },
        {
        day = 3;
        hour = 4;
        value = 0;
    },
        {
        day = 3;
        hour = 5;
        value = 1;
    },
        {
        day = 3;
        hour = 6;
        value = 0;
    },
        {
        day = 3;
        hour = 7;
        value = 0;
    },
        {
        day = 3;
        hour = 8;
        value = 0;
    },
        {
        day = 3;
        hour = 9;
        value = 1;
    },
        {
        day = 3;
        hour = 11;
        value = 0;
    },
        {
        day = 3;
        hour = 15;
        value = 1;
    },
        {
        day = 4;
        hour = 2;
        value = 1;
    },
        {
        day = 4;
        hour = 3;
        value = 1;
    },
        {
        day = 4;
        hour = 4;
        value = 1;
    },
        {
        day = 4;
        hour = 5;
        value = 1;
    },
        {
        day = 4;
        hour = 6;
        value = 1;
    },
        {
        day = 4;
        hour = 7;
        value = 1;
    },
        {
        day = 4;
        hour = 8;
        value = 0;
    },
        {
        day = 4;
        hour = 9;
        value = 1;
    },
        {
        day = 4;
        hour = 11;
        value = 1;
    },
        {
        day = 4;
        hour = 15;
        value = 1;
    },
        {
        day = 5;
        hour = 2;
        value = 1;
    },
        {
        day = 5;
        hour = 3;
        value = 0;
    },
        {
        day = 5;
        hour = 4;
        value = 1;
    },
        {
        day = 5;
        hour = 5;
        value = 0;
    },
        {
        day = 5;
        hour = 6;
        value = 0;
    },
        {
        day = 5;
        hour = 7;
        value = 1;
    },
        {
        day = 5;
        hour = 8;
        value = 1;
    },
        {
        day = 5;
        hour = 9;
        value = 0;
    },
        {
        day = 5;
        hour = 11;
        value = 0;
    },
        {
        day = 5;
        hour = 15;
        value = 1;
    },
        {
        day = 6;
        hour = 7;
        value = 1;
    },
        {
        day = 6;
        hour = 8;
        value = 1;
    },
        {
        day = 6;
        hour = 22;
        value = 1;
    }
)

https://developer.apple.com/documentation/foundation/nspredicate?language=objc

https://developer.apple.com/documentation/foundation/nscompoundpredicate?language=objc

https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Predicates/AdditionalChapters/Introduction.html#//apple_ref/doc/uid/TP40001789

于 2018-09-10T17:58:00.973 回答
0

你可以改变你的数组大小和格式,既然你的数据是清楚的,只需要将数组大小设置为168,并将值0或1直接放入数组中。前 24 个元素将数组放入 day0 值,接下来的 24 个元素放入 day1 值,...,最后 24 个元素放入 day6 值。如果要提取第 3,4,5 天 2-9, 11,15 的值,只需获取 3*6+2 ~ 3*6+9, 4*6+11, 5*6 的元素索引+15 在数组中。

于 2018-09-07T02:57:05.237 回答