Posts

Showing posts from May, 2019

Multiple Inheritance in Swift or Protocol Oriented Programming

Image
Flexibility of protocols in Swift gives us an opportunity to write structs or classes that apply something like multiple inheritance. This article tries to explain a simple example of using that pattern. What is the main purpose of protocols in Swift? Of course protocols talk to structs and classes how they should look. If we signed our data type for the desired protocol and the type has no all the properties and methods which  our protocol needs, then compiler can’t launch the program. Besides we are able to sign one custom data type for many protcols and this possibility allows us to use some patterns from multiple inheritance. It's really cool and safe! For example, let's create a simple app that shows the information about nature object that we randomly get from an array. In the storyboard's scene we need to add an ImageView for object's image or animation, three labels for different information and a button for getting a random object, into the main vie