From the course: iOS Development: Architecture

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Implementing one-way binding, part 1

Implementing one-way binding, part 1

From the course: iOS Development: Architecture

Start my 1-month free trial

Implementing one-way binding, part 1

- [Narrator] In the following demo, I'm going to show you a way to implement one-way binding from the model view to the view. So let's get started. I've gone ahead and made some changes to the MVP demo. I renamed the presenter class and all its instances to ViewModel. Swift doesn't provide an out-of-the-box solution for property binding, so we'll introduce a (mumble) class. I'll add a new Swift source file called Binding. The (mumble) class is called Observable. I'm going to make it generic, so that it can store any type. It has a property called Value. I set up the didSet property observer to get notified whenever the value changes. We'll also need the bind property. The bind property is a closure. I assign an empty closure as default value. We call the bind closure and test the value whenever didSet gets involved. We also need an initializer. The value argument is the observed object. So this is the Observable class. Next, we're going to refactor the ViewModel class.

Contents