-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating

Swift 4 Programming Cookbook

In the previous recipes, we investigated how we can use generics within types and functions. Now we will round off our journey through generics in Swift by looking at how Swift can be used in protocols to produce abstract interfaces, while maintaining strongly typed requirements that allow for a more descriptive model.
In this recipe, we will build a model for a transport app in the UK with the goal of providing the distance that a journey may take over different methods of transport.
At the outset, it may not be clear as to what is the best structure to use for defining a transport method; indeed, there might be different structures appropriate for different methods. Therefore, we will define a transport method as a protocol that appropriate types can conform to:
protocol TransportMethod { associatedtype CollectionPoint var defaultCollectionPoint: CollectionPoint { get } var averageSpeedInKPH: Double { get } }
We define...