正如@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