Avoiding Double Stuffed View Controllers in MVC

Imagine you’re building a house. After months of waiting you sit down with the architect to see the plans. Unfortunately there’s a problem. Everything you asked for is there but it’s all in one giant room! “I can’t take a poo in front of my spouse. It’s a line we don’t cross.” – you remark. Just as designing a building requires dedicated rooms for dedicated  purposes so does code. Model-View-Controller is a way to architect code into 3 distinct layers that achieve 3 separate purposes. Along with compartmentalizing code MVC also lays out a structure for how these layers should communicate with each other. The nature of Apple’s UIKit framework makes it easy to separate objects in the view layer from the controller layer. However, because it’s not strictly enforced, there’s a tendency to put app logic and data in the vcs (view controllers), leaving them looking “double stuffed”.

Structs, What Are They Good For?

There are four scrumptious flavors of named types in Swift: protocols, enumerations, structures and classes – all custom data types that can be created and given a name. Of these, enums, classes and structs can be used to model data, protocols being used to tell the others how to behave. The choice between using a struct and a class has some interesting nuances. The hope of this post is to clear up these murky waters leaving us in a golden shower of data model clarity!