Skip to content

Latest commit

 

History

History
29 lines (20 loc) · 810 Bytes

File metadata and controls

29 lines (20 loc) · 810 Bytes

KVO

Short example

class Person: NSObject {
    @objc dynamic var name = "Tailor Swift"
}

class ViewController: UIViewController {
    
    @objc let somePerson = Person()
    
    var nameObservation: NSKeyValueObservation?

    override func viewDidLoad() {
        super.viewDidLoad()
       
        nameObservation = observe(\.somePerson.name, options: [.new, .old]) { (vc, change) in
            print(change.oldValue, " renamed to ", change.newValue)
        }
        
        somePerson.name = "Justin Bieber"
    }
}