我有以下格式的字典数组。
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 ...
答案 0 :(得分: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。
答案 1 :(得分: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