What I really learn most in this section is debugging and patience…
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if context != &KVOContext {
// If the context does not match, this message must be intended for our superclass.
super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
return
}
guard let keyPath = keyPath, let change = change, object is Employee else {
return
}
var oldValue = change[NSKeyValueChangeKey.oldKey] as Any?
if oldValue is NSNull {
oldValue = nil
}
let undo: UndoManager = undoManager!
Swift.print("oldValue=\(oldValue)")
undo.registerUndo(withTarget: self) { (myself) in
(object as! Employee).setValue(oldValue, forKeyPath: keyPath)
}
undo.setActionName("Typing")
}