AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |
Back to Blog
Revert to xcode 10.31/1/2024 ![]() ![]() And here’s where protocols come into the play!īy 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. 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). Methods that should be written once and used everywhere and with any type. However, the downside is that all of them require methods to perform file operations (saving, loading, deleting). The first step towards that was easy: Creating classes or structures with properties that represent app settings and user preferences. What I needed was app-specific solutions tailored to the needs of each application I was making. User Defaults are undeniably good, but it’s a general and a non-“Swifty” solution (and to be honest, I’ve rarely used them). 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. 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. ![]() 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. 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. And that feature is what actually makes protocols quite powerful and a popular topic in Swift development. What makes protocols particularly interesting is the ability to provide a default implementation for the defined requirements simply by just extending them (protocols). All those defined methods and properties are called requirements. In fewer words, a Swift protocol defines a number of methods and properties that the adopting types (classes, structures, enumerations) must implement. Any type that satisfies the requirements of a protocol is said to conform to that protocol. The protocol can then be adopted by a class, structure, or enumeration to provide an actual implementation of those requirements. Protocols can be used to serve various purposes, however, what remains always the same is what the documentation from Apple says:Ī protocol defines a blueprint of methods, properties, and other requirements that suit a particular task or piece of functionality. 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. ![]()
0 Comments
Read More
Leave a Reply. |