Refactoring to respect Model-View-Controller
We already made some good progress on the core functionality of our app. However, before we move any further, we should reflect on the code we have written. Ultimately, we haven't actually written that many lines of code, but we can definitely improve on this. The biggest shortcoming of our code is that we have put a lot of business logic inside our view controller. This is not a good separation of our different model, view, and controller layers. Let's take this opportunity to refactor this code into a separate type.
We will create a class called PhotoStore
that will be responsible for storing our photos and that will implement the data source protocol. This means that we have to move some of our code out of our view controller, which is exactly our goal.
To do this, first, we will move the photos property to the PhotoStore
class:
import UIKit class PhotoStore: NSObject { var photos = [Photo]() }
Note that this new photo store class...