Observing without bindings
We haven't yet quite caught up with the table we built in the previous chapter using Interface Builder.
At the moment, we are manually triggering the table view's reloadData
method. If we were to continue with that approach, we would need to ensure that we triggered the reload every time something in the data changed that needed to be reflected in the table view.
Our previous version used bindings in Interface Builder to do this automatically, and we are able to reproduce this functionality through the use of the key-value observing mechanism, which we also saw in the last chapter.
Adding KVO to the View Controller
The first step is to add two properties to the ViewController
class:
class ViewController: NSViewController { var tableView: NSTableView! var infoLabel:NSTextField! var peopleArrayWrapper = PeopleArrayWrapper(content: []) let kContentKeyPath = "content" private var myContext = 0 ...
We need these to add the ViewController
class as an observer...