This repository has been archived by the owner on Jul 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NSPredicate+Extra.m
63 lines (59 loc) · 2.13 KB
/
NSPredicate+Extra.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//
// NSPredicate+Extra.m
// VK Omnia
//
// Created by Евгений Кратько on 04.04.13.
// Copyright (c) 2013 Akki. All rights reserved.
//
#import "NSPredicate+Extra.h"
@implementation NSPredicate (Extra)
- (NSPredicate *)predicateByRemovingSubpredicate:(NSPredicate *)sub error:(NSError *__autoreleasing *)error
{
NSMutableArray *subpredicates = [[NSPredicate simpleSubpredicatesFrom:self] mutableCopy];
if ((subpredicates.count > 1) && [self isKindOfClass:[NSCompoundPredicate class]]) {
NSCompoundPredicateType type = [(NSCompoundPredicate*)self compoundPredicateType];
if ([subpredicates containsObject:sub]) {
[subpredicates removeObject:sub];
NSPredicate *result = [[NSCompoundPredicate alloc] initWithType:type subpredicates:subpredicates];
return result;
}
else {
*error = [NSError errorWithDomain:@"Predicate doesn't contains given subpredicate" code:0 userInfo:nil];
return self;
}
}
else if (subpredicates.count == 1) {
if ([[[subpredicates objectAtIndex:0] predicateFormat] isEqualToString:sub.predicateFormat]) {
return nil;
}
else {
*error = [NSError errorWithDomain:@"Predicate doesn't contains given subpredicate" code:0 userInfo:nil];
return self;
}
}
else
return nil;
}
+ (NSArray*)simpleSubpredicatesFrom:(NSPredicate*)predicate
{
if ([predicate isKindOfClass:[NSCompoundPredicate class]]) {
NSCompoundPredicate *source = (NSCompoundPredicate*)predicate;
NSMutableArray *result = [NSMutableArray new];
if (source.subpredicates.count == 1) {
[result addObject:source];
}
else {
for (NSPredicate *pred in [source subpredicates]) {
if (![pred isKindOfClass:[NSCompoundPredicate class]]) {
[result addObject:pred];
}
else {
[result addObjectsFromArray:[self simpleSubpredicatesFrom:pred]];
}
}
}
return (NSArray*)result;
}
return nil;
}
@end