Question: To learn KVO, I copied the code from <https://developer.apple.com/documentation/swift/cocoa_design_patterns/using_key_value_observing_in_swift>. It is as follows 

```
 class MyObjectToObserve: NSObject {
        @objc dynamic var myDate = NSDate(timeIntervalSince1970: 0) // 1970
        func updateDate() {
            myDate = myDate.addingTimeInterval(Double(2 << 30)) // Adds about 68 years.
        }
    }

    class MyObserver: NSObject {
        @objc var objectToObserve: MyObjectToObserve
        var observation: NSKeyValueObservation?

        init(object: MyObjectToObserve) {
            objectToObserve = object
            super.init()

            observation = observe(
                \.objectToObserve.myDate,
                options: [.old, .new]
            ) { object, change in
                print("myDate changed from: \(change.oldValue!), updated to: \(change.newValue!)")
            }
        }
    }

    let observed = MyObjectToObserve()
    let observer = MyObserver(object: observed)

    observed.updateDate()

```

The second to the last line will cause an warning since `observer` is not used. After I replace `let observer` with `_` as suggested by Xcode, warning is gone but it will give an runtime error:

Thread 1: EXC\_BAD\_ACCESS (code=EXC\_I386\_GPFLT)

My Goal is to get answer to the following questions:

1.Does anyone know why it causes an error? 

2.How to resolve the warning?

3.Is this example UP-TO-DATE for KVO in Swift? Syntax level?

Answer: 
The code snippets in Apples documentation are for example only - that's not a complete implementation.

Presumably, you will be doing something else with `observer`, at which point you will no longer get the "not used" warnings.

Sample usage (just for demonstration, not to be considered production code):

```
class ExampleViewController: UIViewController {

    var observed: MyObjectToObserve!
    var observer: MyObserver!

    override func viewDidLoad() {
        super.viewDidLoad()

        observed = MyObjectToObserve()
        observer = MyObserver(object: observed)

    }

    @IBAction func didTap(_ sender: Any) {
        observed.updateDate()
    }

}

```

Create a new view controller; set its class to `ExampleViewController`; add a button and connect it to `@IBAction func didTap`.

Run the app.. each time you tap the button, you will see the `print()` output in the debug console.
The irony is that Apple's example is more complex than it needs to be. That is, the `MyObserver` object isn't even necessary. You can simply call `observe` on the object you are actually observing.

This looks like the following:

```
class MyObjectToObserve: NSObject {
    @objc dynamic var myDate = NSDate(timeIntervalSince1970: 0) // 1970
    func updateDate() {
        myDate = myDate.addingTimeInterval(Double(2 << 30)) // Adds about 68 years.
    }
}

let observed = MyObjectToObserve()
let observation = observed.observe(\MyObjectToObserve.myDate, options: [.old, .new]) { object, change in
    print("myDate changed from: \(change.oldValue!), updated to: \(change.newValue!)")
}

observed.updateDate()

```

Unfortunately, this simpler version still has the original problem. You need `let observation = ...` to keep it in scope for KVO to work, and you get the warning.

So to suppress the warning, you need to do something with the result. Add the following line:

```
_ = observation.hash    // Suppress warning on observation

```

Hopefully the compiler would optimize that out (and if not it is just assigning an integer so the overhead is negligible), but it does serve to suppress the warning. I include the comment so that the next programmer to look at the code doesn't remove the seemingly useless line.

*Note:* I rarely use KVO any more as I find Combine to be both more powerful and less troublesome. However, sometimes it remains necessary. Even then in most cases my code looks more like that of @DonMag where a reference to the necessary object is kept as a member of a controller. The one place where I do use this suppression "hack" is in some of my unit tests where I am manually setting up the KVO and don't want my code showing warnings.