Undo for Edits (p197)

Hi there, I managed to get the undo for edit works with following code:

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
    if context != &KVOContext {
        super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
    let oldValue = change?[.oldKey]

    if let undo = self.undoManager {
        undo.registerUndo(withTarget: object as! Employee) {
            target in
            target.setValue(oldValue, forKeyPath: keyPath!)

Just wonder is there any other solution for this. Thank you.


Your registerUndo() really helps me understand its syntax better.