/
NSPredicateExtensions.swift
76 lines (64 loc) · 2.38 KB
/
NSPredicateExtensions.swift
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
64
65
66
67
68
69
70
71
72
73
74
75
76
// NSPredicateExtensions.swift - Copyright 2024 SwifterSwift
#if canImport(Foundation)
import Foundation
// MARK: - Properties
public extension NSPredicate {
/// SwifterSwift: Returns a new predicate formed by NOT-ing the predicate.
var not: NSCompoundPredicate {
return NSCompoundPredicate(notPredicateWithSubpredicate: self)
}
}
// MARK: - Methods
public extension NSPredicate {
/// SwifterSwift: Returns a new predicate formed by AND-ing the argument to the predicate.
///
/// - Parameter predicate: NSPredicate.
/// - Returns: NSCompoundPredicate.
func and(_ predicate: NSPredicate) -> NSCompoundPredicate {
return NSCompoundPredicate(andPredicateWithSubpredicates: [self, predicate])
}
/// SwifterSwift: Returns a new predicate formed by OR-ing the argument to the predicate.
///
/// - Parameter predicate: NSPredicate.
/// - Returns: NSCompoundPredicate.
func or(_ predicate: NSPredicate) -> NSCompoundPredicate {
return NSCompoundPredicate(orPredicateWithSubpredicates: [self, predicate])
}
}
// MARK: - Operators
public extension NSPredicate {
/// SwifterSwift: Returns a new predicate formed by NOT-ing the predicate.
/// - Parameters: rhs: NSPredicate to convert.
/// - Returns: NSCompoundPredicate
static prefix func ! (rhs: NSPredicate) -> NSCompoundPredicate {
return rhs.not
}
/// SwifterSwift: Returns a new predicate formed by AND-ing the argument to the predicate.
///
/// - Parameters:
/// - lhs: NSPredicate.
/// - rhs: NSPredicate.
/// - Returns: NSCompoundPredicate
static func + (lhs: NSPredicate, rhs: NSPredicate) -> NSCompoundPredicate {
return lhs.and(rhs)
}
/// SwifterSwift: Returns a new predicate formed by OR-ing the argument to the predicate.
///
/// - Parameters:
/// - lhs: NSPredicate.
/// - rhs: NSPredicate.
/// - Returns: NSCompoundPredicate
static func | (lhs: NSPredicate, rhs: NSPredicate) -> NSCompoundPredicate {
return lhs.or(rhs)
}
/// SwifterSwift: Returns a new predicate formed by remove the argument to the predicate.
///
/// - Parameters:
/// - lhs: NSPredicate.
/// - rhs: NSPredicate.
/// - Returns: NSCompoundPredicate
static func - (lhs: NSPredicate, rhs: NSPredicate) -> NSCompoundPredicate {
return lhs + !rhs
}
}
#endif