
This video gives an overview of the entire course.
In this video, we will look at how to define a protocol, how to add requirements to it and how protocol inheritance works.
Even though no functionality is implemented in a protocol, they are still considered a full-fledged type in the Swift programming language, and can mostly be used like any other type.
Polymorphism lets us interact with multiple types through a single uniform interface. In the object-oriented programming world, the single uniform interface usually comes from a superclass, while in the protocol-oriented programming world, that single interface usually comes from a protocol.
Type casting is a way to check the type of an instance and/or to treat the instance as a specified type. Also, when defining a protocol, there are times when it is useful to define one or more associated types. We’ll see that as well in this video.
Delegation is used extensively within the Cocoa and Cocoa Touch frameworks. The delegation pattern is a very simple but powerful pattern where an instance of one type acts on behalf of another instance.
With protocol-oriented programming, we should always begin our design with the protocols, but how should we design these protocols? Let’ see that in this video.
In object-oriented programming, we cannot create an object without a blueprint that tells the application what properties and methods to expect from the object. In most object-oriented languages, this blueprint comes in the form of a class.
Access controls allow us to restrict the access to, and visibility of, parts of our code. This allows us to hide implementation details and only expose the interfaces we want the external code to access.
In Swift, a tuple is a finite, ordered, comma-separated list of elements. Tuples are one of the most underutilized types in Swift and, let’s look at them in detail.
There are some very fundamental differences between value types (structures, enumerations, and tuples) and reference types (classes). The primary difference is how the instances of value and reference types are passed.
A recursive data type is a type that contains other values of the same type as a property for the type. Recursive data types are used when we want to define dynamic data structures, such as lists and trees.
The process of choosing which implementation to call is performed at runtime and is known as dynamic dispatch. Let’s explore dynamic dispatch in this video.
An extension is defined by using the extension keyword followed by the name of the type you are extending. We then put the functionality that we are adding to the type between curly brackets.
Protocols, like other types, can be extended. Protocol extensions can be used to provide common functionality to all types that conform to a particular protocol. This gives us the ability to add functionality to any type that conforms to a protocol rather than adding the functionality to each individual type or through a global function.
In numerous apps, across multiple platforms, we are often asked to validate user input either after the user has entered it. This validation can be done very easily with regular expressions; however, we do not want various regular expression strings littered throughout our code. We can solve this problem by creating different classes or structures that contain the validation code.
Let's say that, in our application, we needed to calculate the factorial of some integers. A factorial is written as 5! To calculate a factorial, we take the product of all the positive integers that are less than or equal to the number. Take a look at how we do that with extensions.
In this video, we will show how we can conform to the Equatable protocol using extensions. When a type conforms to the Equatable protocol, we can use the equal-to (==) operator to compare for equality and the not-equal-to (! =) operator to compare for inequality.
To fully understand generics, we need to understand the problem that they are designed to solve.
A generic type is a class, structure, or enumeration that can work with any type, just like Swift arrays and optionals can work with any type.
An associated type declares a placeholder name that can be used instead of a type within a protocol. The actual type to be used is not specified until the protocol itself is adopted.
Prior to Swift version 4, we could use generics with subscripts only if the generic was defined in the containing type, however we were unable to define a new generic type within the subscript definition.
Apple has implemented the COW feature for all the data structures (Array, Dictionary, and Set) within the Swift standard library. With COW, Swift does not make a second copy of the data structure until a change is made to that data structure.
Now that we have seen how to use generics, let's see how we can use them in a protocol-oriented design.
Swift provides full support for developing applications in an object-oriented way. Prior to Swift 2, Swift is primarily an object-oriented language in the same way as Java and C#. Here, we will be designing the vehicle types in an object-oriented way and looking at the advantages and the disadvantages of this design.
Two of the issues that we saw with the object-oriented design were directly related to each other, and are the result of Swift being a single-inheritance language. Remember a single-inheritance language is a language that limits a class to having not more than one superclass. An object-oriented design with a single-inheritance language, such as Swift, can lead to bloated superclasses because we may need to include functionality that is needed by only a few of the subclasses. This leads to the second issue related to Swift being a single-inheritance language, which is the inheritance of functionality that a type does not need.
Now that we know the object-oriented design, let's look at how we could design the vehicles in a protocol-oriented way.
In the object-oriented programming example, we created a Vehicle superclass from which all the vehicle classes were derived from. In the protocol-oriented programming example, we used a combination of protocols and protocol extensions to achieve the same result; however, there are several advantages to the protocol-oriented design.
Creational patterns are design patterns that deal with how an object is created. There are two basic ideas behind creational patterns. The first is encapsulating the knowledge of which concrete types should be created and the second is hiding how instances of these types are created.
Structural design patterns describe how types can be combined to form larger structures. These larger structures can generally be easier to work with and hide a lot of the complexity of the individual types.
Behavioral design patterns explain how types interact with each other. These patterns describe how different instances of types send messages to each other to make something happen.
Logging frameworks make it incredibly effortless to turn on log messages which makes debugging very easy while the application is being developed. These debugging messages can then be turned off when it is time to build the production release of the application. To do this, these logging frameworks let us define how and where we wish to log the messages for predefined log levels.
he most serious applications need to persist some amounts of data. This data could be transactional data, user preferences, or the current state of the application. There are many ways that data can be persisted in our applications. Let’s see how we choose the correct way.
Swift has become the number one language used in iOS and macOS development. The Swift standard library is developed using Protocol-Oriented Programming techniques, generics, and first-class value semantics; therefore, every Swift developer should understand these powerful concepts and how to take advantage of them in their application design.
This course will help you understand the differences between Object-Oriented Programming and Protocol-Oriented Programming. It demonstrates how to work with Protocol-Oriented Programming using real-world use cases. You will gain a solid knowledge of the various types that can be used in Swift and the differences between value and reference types. You will be taught how Protocol-Oriented Programming techniques can be used to develop very flexible and easy-to-maintain code.
By the end of the course, you will have a thorough understanding of Protocol-Oriented Programming and how to utilize it to build powerful and practical applications.
About the authr
Jon Hoffman has over 25 years of experience in the field of information technology. Over these years, Jon has worked in the areas of system administration, network administration, network security, application development, and architecture. Currently, Jon works as a senior software engineer for Syn-Tech Systems.
Jon has developed extensively for the iOS platform since 2008. This includes several apps that he has published in the App Store, apps that he has written for third parties, and numerous enterprise applications. He has also developed mobile applications for the Android and Windows platforms. What really drives Jon the challenges that the field of information technology provides and there is nothing more exhilarating to him than overcoming a challenge.
Some of Jon’s other interests are spending time with his family, robotic projects, and 3D printing. Jon also really enjoys Tae Kwon Do, where he and his oldest daughter Kailey earned their black belts together early in 2014, Kim (his wife) earned her black belt in December 2014, and his youngest daughter Kara is currently working towards her black belt.