@@ -35,34 +35,34 @@ public protocol NSKeyValueObservingCustomization : NSObjectProtocol {
35
35
36
36
fileprivate extension NSObject {
37
37
38
- @objc class func _old_unswizzled_automaticallyNotifiesObservers ( forKey key: String ? ) -> Bool {
38
+ @objc class func __old_unswizzled_automaticallyNotifiesObservers ( forKey key: String ? ) -> Bool {
39
39
fatalError ( " Should never be reached " )
40
40
}
41
41
42
- @objc class func _old_unswizzled_keyPathsForValuesAffectingValue ( forKey key: String ? ) -> Set < String > {
42
+ @objc class func __old_unswizzled_keyPathsForValuesAffectingValue ( forKey key: String ? ) -> Set < String > {
43
43
fatalError ( " Should never be reached " )
44
44
}
45
45
46
46
}
47
47
48
- @objc private class _KVOKeyPathBridgeMachinery : NSObject {
48
+ @objc private class __KVOKeyPathBridgeMachinery : NSObject {
49
49
@nonobjc static var keyPathTable : [ String : AnyKeyPath ] = {
50
50
/*
51
51
Move all our methods into place. We want the following:
52
- _KVOKeyPathBridgeMachinery 's automaticallyNotifiesObserversForKey:, and keyPathsForValuesAffectingValueForKey: methods replaces NSObject's versions of them
52
+ __KVOKeyPathBridgeMachinery 's automaticallyNotifiesObserversForKey:, and keyPathsForValuesAffectingValueForKey: methods replaces NSObject's versions of them
53
53
NSObject's automaticallyNotifiesObserversForKey:, and keyPathsForValuesAffectingValueForKey: methods replace NSObject's _old_unswizzled_* methods
54
- NSObject's _old_unswizzled_* methods replace _KVOKeyPathBridgeMachinery 's methods, and are never invoked
54
+ NSObject's _old_unswizzled_* methods replace __KVOKeyPathBridgeMachinery 's methods, and are never invoked
55
55
*/
56
56
let rootClass : AnyClass = NSObject . self
57
- let bridgeClass : AnyClass = _KVOKeyPathBridgeMachinery . self
57
+ let bridgeClass : AnyClass = __KVOKeyPathBridgeMachinery . self
58
58
59
59
let dependentSel = #selector( NSObject . keyPathsForValuesAffectingValue ( forKey: ) )
60
60
let rootDependentImpl = class_getClassMethod ( rootClass, dependentSel) !
61
61
let bridgeDependentImpl = class_getClassMethod ( bridgeClass, dependentSel) !
62
62
method_exchangeImplementations ( rootDependentImpl, bridgeDependentImpl) // NSObject <-> Us
63
63
64
64
let originalDependentImpl = class_getClassMethod ( bridgeClass, dependentSel) ! //we swizzled it onto this class, so this is actually NSObject's old implementation
65
- let originalDependentSel = #selector( NSObject . _old_unswizzled_keyPathsForValuesAffectingValue ( forKey: ) )
65
+ let originalDependentSel = #selector( NSObject . __old_unswizzled_keyPathsForValuesAffectingValue ( forKey: ) )
66
66
let dummyDependentImpl = class_getClassMethod ( rootClass, originalDependentSel) !
67
67
method_exchangeImplementations ( originalDependentImpl, dummyDependentImpl) // NSObject's original version <-> NSObject's _old_unswizzled_ version
68
68
@@ -72,7 +72,7 @@ fileprivate extension NSObject {
72
72
method_exchangeImplementations ( rootAutoImpl, bridgeAutoImpl) // NSObject <-> Us
73
73
74
74
let originalAutoImpl = class_getClassMethod ( bridgeClass, autoSel) ! //we swizzled it onto this class, so this is actually NSObject's old implementation
75
- let originalAutoSel = #selector( NSObject . _old_unswizzled_automaticallyNotifiesObservers ( forKey: ) )
75
+ let originalAutoSel = #selector( NSObject . __old_unswizzled_automaticallyNotifiesObservers ( forKey: ) )
76
76
let dummyAutoImpl = class_getClassMethod ( rootClass, originalAutoSel) !
77
77
method_exchangeImplementations ( originalAutoImpl, dummyAutoImpl) // NSObject's original version <-> NSObject's _old_unswizzled_ version
78
78
@@ -83,62 +83,62 @@ fileprivate extension NSObject {
83
83
84
84
@nonobjc fileprivate static func _bridgeKeyPath( _ keyPath: AnyKeyPath ) -> String {
85
85
guard let keyPathString = keyPath. _kvcKeyPathString else { fatalError ( " Could not extract a String from KeyPath \( keyPath) " ) }
86
- _KVOKeyPathBridgeMachinery . keyPathTableLock. lock ( )
87
- defer { _KVOKeyPathBridgeMachinery . keyPathTableLock. unlock ( ) }
88
- _KVOKeyPathBridgeMachinery . keyPathTable [ keyPathString] = keyPath
86
+ __KVOKeyPathBridgeMachinery . keyPathTableLock. lock ( )
87
+ defer { __KVOKeyPathBridgeMachinery . keyPathTableLock. unlock ( ) }
88
+ __KVOKeyPathBridgeMachinery . keyPathTable [ keyPathString] = keyPath
89
89
return keyPathString
90
90
}
91
91
92
92
@nonobjc fileprivate static func _bridgeKeyPath( _ keyPath: String ? ) -> AnyKeyPath ? {
93
93
guard let keyPath = keyPath else { return nil }
94
- _KVOKeyPathBridgeMachinery . keyPathTableLock. lock ( )
95
- defer { _KVOKeyPathBridgeMachinery . keyPathTableLock. unlock ( ) }
96
- let path = _KVOKeyPathBridgeMachinery . keyPathTable [ keyPath]
94
+ __KVOKeyPathBridgeMachinery . keyPathTableLock. lock ( )
95
+ defer { __KVOKeyPathBridgeMachinery . keyPathTableLock. unlock ( ) }
96
+ let path = __KVOKeyPathBridgeMachinery . keyPathTable [ keyPath]
97
97
return path
98
98
}
99
99
100
100
@objc override class func automaticallyNotifiesObservers( forKey key: String ) -> Bool {
101
101
//This is swizzled so that it's -[NSObject automaticallyNotifiesObserversForKey:]
102
- if let customizingSelf = self as? NSKeyValueObservingCustomization . Type , let path = _KVOKeyPathBridgeMachinery . _bridgeKeyPath ( key) {
102
+ if let customizingSelf = self as? NSKeyValueObservingCustomization . Type , let path = __KVOKeyPathBridgeMachinery . _bridgeKeyPath ( key) {
103
103
return customizingSelf. automaticallyNotifiesObservers ( for: path)
104
104
} else {
105
- return self . _old_unswizzled_automaticallyNotifiesObservers ( forKey: key) //swizzled to be NSObject's original implementation
105
+ return self . __old_unswizzled_automaticallyNotifiesObservers ( forKey: key) //swizzled to be NSObject's original implementation
106
106
}
107
107
}
108
108
109
109
@objc override class func keyPathsForValuesAffectingValue( forKey key: String ? ) -> Set < String > {
110
110
//This is swizzled so that it's -[NSObject keyPathsForValuesAffectingValueForKey:]
111
- if let customizingSelf = self as? NSKeyValueObservingCustomization . Type , let path = _KVOKeyPathBridgeMachinery . _bridgeKeyPath ( key!) {
111
+ if let customizingSelf = self as? NSKeyValueObservingCustomization . Type , let path = __KVOKeyPathBridgeMachinery . _bridgeKeyPath ( key!) {
112
112
let resultSet = customizingSelf. keyPathsAffectingValue ( for: path)
113
113
return Set ( resultSet. lazy. map {
114
114
guard let str = $0. _kvcKeyPathString else { fatalError ( " Could not extract a String from KeyPath \( $0) " ) }
115
115
return str
116
116
} )
117
117
} else {
118
- return self . _old_unswizzled_keyPathsForValuesAffectingValue ( forKey: key) //swizzled to be NSObject's original implementation
118
+ return self . __old_unswizzled_keyPathsForValuesAffectingValue ( forKey: key) //swizzled to be NSObject's original implementation
119
119
}
120
120
}
121
121
}
122
122
123
123
func _bridgeKeyPathToString( _ keyPath: AnyKeyPath ) -> String {
124
- return _KVOKeyPathBridgeMachinery . _bridgeKeyPath ( keyPath)
124
+ return __KVOKeyPathBridgeMachinery . _bridgeKeyPath ( keyPath)
125
125
}
126
126
127
127
func _bridgeStringToKeyPath( _ keyPath: String ) -> AnyKeyPath ? {
128
- return _KVOKeyPathBridgeMachinery . _bridgeKeyPath ( keyPath)
128
+ return __KVOKeyPathBridgeMachinery . _bridgeKeyPath ( keyPath)
129
129
}
130
130
131
- public class NSKeyValueObservation : NSObject {
131
+ public class _NSKeyValueObservation : NSObject {
132
132
133
133
@nonobjc weak var object : NSObject ?
134
134
@nonobjc let callback : ( NSObject , NSKeyValueObservedChange < Any > ) -> Void
135
135
@nonobjc let path : String
136
136
137
137
//workaround for <rdar://problem/31640524> Erroneous (?) error when using bridging in the Foundation overlay
138
- @nonobjc static var swizzler : NSKeyValueObservation ? = {
139
- let bridgeClass : AnyClass = NSKeyValueObservation . self
138
+ @nonobjc static var swizzler : _NSKeyValueObservation ? = {
139
+ let bridgeClass : AnyClass = _NSKeyValueObservation . self
140
140
let observeSel = #selector( NSObject . observeValue ( forKeyPath: of: change: context: ) )
141
- let swapSel = #selector( NSKeyValueObservation . _swizzle_me_observeValue ( forKeyPath: of: change: context: ) )
141
+ let swapSel = #selector( _NSKeyValueObservation . _swizzle_me_observeValue ( forKeyPath: of: change: context: ) )
142
142
let rootObserveImpl = class_getInstanceMethod ( bridgeClass, observeSel)
143
143
let swapObserveImpl = class_getInstanceMethod ( bridgeClass, swapSel)
144
144
method_exchangeImplementations ( rootObserveImpl, swapObserveImpl)
@@ -147,7 +147,7 @@ public class NSKeyValueObservation : NSObject {
147
147
148
148
fileprivate init ( object: NSObject , keyPath: AnyKeyPath , callback: @escaping ( NSObject , NSKeyValueObservedChange < Any > ) -> Void ) {
149
149
path = _bridgeKeyPathToString ( keyPath)
150
- let _ = NSKeyValueObservation . swizzler
150
+ let _ = _NSKeyValueObservation . swizzler
151
151
self . object = object
152
152
self . callback = callback
153
153
}
@@ -156,7 +156,7 @@ public class NSKeyValueObservation : NSObject {
156
156
object? . addObserver ( self , forKeyPath: path, options: options, context: nil )
157
157
}
158
158
159
- ///invalidate() will be called automatically when an NSKeyValueObservation is deinited
159
+ ///invalidate() will be called automatically when an _NSKeyValueObservation is deinited
160
160
@objc public func invalidate( ) {
161
161
object? . removeObserver ( self , forKeyPath: path, context: nil )
162
162
object = nil
@@ -181,13 +181,13 @@ public class NSKeyValueObservation : NSObject {
181
181
182
182
extension _KeyValueCodingAndObserving {
183
183
184
- ///when the returned NSKeyValueObservation is deinited or invalidated, it will stop observing
184
+ ///when the returned _NSKeyValueObservation is deinited or invalidated, it will stop observing
185
185
public func observe< Value> (
186
186
_ keyPath: KeyPath < Self , Value > ,
187
187
options: NSKeyValueObservingOptions = [ ] ,
188
188
changeHandler: @escaping ( Self , NSKeyValueObservedChange < Value > ) -> Void )
189
- -> NSKeyValueObservation {
190
- let result = NSKeyValueObservation ( object: self as! NSObject , keyPath: keyPath) { ( obj, change) in
189
+ -> _NSKeyValueObservation {
190
+ let result = _NSKeyValueObservation ( object: self as! NSObject , keyPath: keyPath) { ( obj, change) in
191
191
let notification = NSKeyValueObservedChange ( kind: change. kind,
192
192
newValue: change. newValue as? Value ,
193
193
oldValue: change. oldValue as? Value ,
0 commit comments