The KVO and swift attributes observer

The opening note: the basics of KVO and its KVO in OC are available: go deep into runtime, explore KVO

Swift, there is no KVO model, why do you say, please see below:

KVO is essentially a runtime based dynamic distribution mechanism that listens to the value of value through key.
OC can monitor because are in compliance with the NSKeyValueCoding protocol
OC all the classes are inherited from NSObject, the default has complied with the agreement, but Swift is not based on runtime, the property of Swift in performance and other aspects of the consideration is the closed dynamic distribution of default, only in the property before the dynamic will start operation when changes are allowed to listen to the property.

The difference between KVO in OC and Swift:

In OC, all classes inherit from NSObject, which provides a default implementation for KVO, but Swift is not.
for two reasons:

First: the attributes inherited from NSObject in Swift are in performance, and the default is to shut down the dynamic distribution, so you can’t use KVO, but you can add dynamic to the property before you open it.

Class Person:NSObject {//name does not support KVO listening, age supports KVO, VaR, name:, String, VaR, age:, Int = 0, init (name:, String, age:, Int), { = name, self.age, =age} dynamic

Second: not all classes inherit from NSObject, objects that do not inherit themselves

For example:

Class, Person, {VaR, name:, String, VaR, age:, Int = 0, init (name:, String, age:, Int), { = name, self.age, =age}

This class cannot be called at all

Open func addObserver (observer: NSObject forKeyPath _, keyPath: String, options: NSKeyValueObservingOptions = [], context: UnsafeMutableRawPointer?)

So it cannot use the KVO method, the observer pattern, but Swift provides properties (didSet, willSet) observer to solve this problem;

Class Father: NSObject {var firstName: String = "First" {willSet {/ / new value is set to be called before print ("willSet / (newValue) is a new value")} didSet {/ / new value is set immediately after the call print ("the new value is didSet / (oldValue)")}}}

Normal use of KVO: (“three steps”)

The first step: open func addObserver (_ registered observer: NSObject, forKeyPath keyPath: String, options: NSKeyValueObservingOptions = [], context: UnsafeMutableRawPointer?) second steps: monitoring override (forKeyPath func observeValue keyPath: String of object:?, Any?, change: [NSKeyValueChangeKey, context:: Any]? UnsafeMutableRawPointer? Third) open func removeObserver (step: remove _ observer: NSObject forKeyPath, keyPath: String)

Code demo

Import UIKit / / UISlider Sliding Monitoring pass, sliding results to the UIProgressView, to display the progress of class viewController: UIViewController sliding {@IBOutlet weak var slider: UISlider @IBOutlet weak var progressView:! UIProgressView! Override func (viewDidLoad) {super.viewDidLoad (any) Do additional setup after loading / the view. self.progressView.addObserver (self, forKeyPath:, options: "progress".New, context: nil self.slider.addObserver (self forKeyPath:), "value", options:.New, context: nil override func observeValue (forKeyPath)} keyPath: String? Of, object: Any, change: [NSKeyValueChangeKey? Any]? Context:, UnsafeMutableRawPointer?) {if {keyPath = = "value" If let value = change? [NSKeyValueChangeKey.newKey] as? Float value/self.slider.maximumValue view.alpha = {self.progressView.progress = CGFloat (self.progressView.progress) self.textLabel.font = UIFont.systemFont (ofSize: view.alpha * 20) self.textField.text = self.father?.firstName override func didReceiveMemoryWarning}}}) {(super.didReceiveMemoryWarning) Dispose (of resources that can / / any be recreated.} deinit {self.progressView.removeObserver (self, forKeyPath: "progress self.slider.removeObserver (self), forKeyPath:" value "})}

Detailed GitHub address of Demo