@@ -26,19 +26,60 @@ open class NSPredicate : NSObject, NSSecureCoding, NSCopying {
26
26
}
27
27
28
28
public required init ? ( coder aDecoder: NSCoder ) {
29
- NSUnimplemented ( )
29
+ guard aDecoder. allowsKeyedCoding else {
30
+ preconditionFailure ( " Unkeyed coding is unsupported. " )
31
+ }
32
+
33
+ let encodedBool = aDecoder. decodeBool ( forKey: " NS.boolean.value " )
34
+ self . kind = . boolean( encodedBool)
35
+
36
+ super. init ( )
30
37
}
31
38
32
39
open func encode( with aCoder: NSCoder ) {
33
- NSUnimplemented ( )
40
+ guard aCoder. allowsKeyedCoding else {
41
+ preconditionFailure ( " Unkeyed coding is unsupported. " )
42
+ }
43
+
44
+ switch self . kind {
45
+ case . boolean( let value) :
46
+ aCoder. encode ( value, forKey: " NS.boolean.value " )
47
+ case . block:
48
+ preconditionFailure ( " NSBlockPredicate cannot be encoded or decoded. " )
49
+ }
34
50
}
35
51
36
52
open override func copy( ) -> Any {
37
53
return copy ( with: nil )
38
54
}
39
55
40
56
open func copy( with zone: NSZone ? = nil ) -> Any {
41
- NSUnimplemented ( )
57
+ switch self . kind {
58
+ case . boolean( let value) :
59
+ return NSPredicate ( value: value)
60
+ case . block( let block) :
61
+ return NSPredicate ( block: block)
62
+ }
63
+ }
64
+
65
+ open override func isEqual( _ object: Any ? ) -> Bool {
66
+ if let other = object as? NSPredicate {
67
+ if other === self {
68
+ return true
69
+ } else {
70
+ switch ( other. kind, self . kind) {
71
+ case ( . boolean( let otherBool) , . boolean( let selfBool) ) :
72
+ return otherBool == selfBool
73
+ // TODO: case for init(format:argumentArray:)
74
+ // TODO: case for init(fromMetadataQueryString:)
75
+ // NSBlockPredicate returns false even for copy
76
+ default :
77
+ return false
78
+ }
79
+ }
80
+ }
81
+
82
+ return false
42
83
}
43
84
44
85
// Parse predicateFormat and return an appropriate predicate
@@ -58,7 +99,17 @@ open class NSPredicate : NSObject, NSSecureCoding, NSCopying {
58
99
super. init ( )
59
100
}
60
101
61
- open var predicateFormat : String { NSUnimplemented ( ) } // returns the format string of the predicate
102
+ open var predicateFormat : String {
103
+ switch self . kind {
104
+ case . boolean( let value) :
105
+ return value ? " TRUEPREDICATE " : " FALSEPREDICATE "
106
+ case . block:
107
+ // TODO: Bring NSBlockPredicate's predicateFormat to macOS's Foundation version
108
+ // let address = unsafeBitCast(block, to: Int.self)
109
+ // return String(format:"BLOCKPREDICATE(%2X)", address)
110
+ return " BLOCKPREDICATE "
111
+ }
112
+ }
62
113
63
114
open func withSubstitutionVariables( _ variables: [ String : Any ] ) -> Self { NSUnimplemented ( ) } // substitute constant values for variables
64
115
0 commit comments