Hello and welcome to a new tutorial! One of the most common concepts met and used in Swift by all developers is protocols, and I don’t think there’s even one developer who doesn’t know about them. Protocols can be used to serve various purposes, however, what remains always the same is what the documentation from Apple says:
A protocol defines a blueprint of methods, properties, and other requirements that suit a particular task or piece of functionality. The protocol can then be adopted by a class, structure, or enumeration to provide an actual implementation of those requirements. Any type that satisfies the requirements of a protocol is said to conform to that protocol.
In fewer words, a Swift protocol defines a number of methods and properties that the adopting types (classes, structures, enumerations) must implement. All those defined methods and properties are called requirements.
What makes protocols particularly interesting is the ability to provide a default implementation for the defined requirements simply by just extending them (protocols). And that feature is what actually makes protocols quite powerful and a popular topic in Swift development. By being able to define a series of methods that “describe” a set of functionalities and have a standard implementation of them, classes, structures and enumerations that are not even related among them (the opposite of what happens with inheritance in classes for example) can acquire common, additional features which extend their functionalities.
If you are a new developer, then I really encourage you to read more on protocols as there’s a lot of interesting information to find out.
Now, what protocols have to do with app settings and anything that the title of this post says? Well, let me explain and make the connection. For a long time, I was bothered by the fact that UserDefaults has been the only mechanism to save small bits of data fast, such as settings and user preferences. User Defaults are undeniably good, but it’s a general and a non-“Swifty” solution (and to be honest, I’ve rarely used them). What I needed was app-specific solutions tailored to the needs of each application I was making. The first step towards that was easy: Creating classes or structures with properties that represent app settings and user preferences. However, the downside is that all of them require methods to perform file operations (saving, loading, deleting). Methods that should be written once and used everywhere and with any type. Using classes and inheritance with a parent class to implement the standard functionality was out of the table, as it would rule out structures (structs
). And here’s where protocols come into the play!
By having a protocol defining a series of methods that will be responsible for file manipulation and a protocol extension to provide a default implementation, every single class or structure adopting it would get the same file-related functionalities! And not just that. Multiple classes and structures supporting various kind of settings can co-exist in the same app that way, and various parts of data can be handled separately. Using different words, such a protocol is like a plug-and-play mechanism which allows to save and load data on any custom type that adopts it.
This is a solution that I’ve been using for a long time, and it’s now time to discuss it here. Obviously, the content of this post is addressed mostly to new Swift developers as it will help to get their head around protocols and see an actual use of them. Advanced developers have undoubtedly come up with solutions like that already, but regardless, everyone is encouraged to keep on reading!