November 2016
Intermediate to advanced
480 pages
14h 42m
English
You will need to create a new type before you can add protocol conformance to it through an extension. Make a new struct to represent a Car type. You will later use an extension on Car to add conformance to the Vehicle protocol.
Listing 21.4 A Car struct
...
typealias Velocity = Double
extension Velocity {
var kph: Velocity { return self * 1.60934 }
var mph: Velocity { return self }
}
protocol Vehicle {
var topSpeed: Velocity { get }
var numberOfDoors: Int { get }
var hasFlatbed: Bool { get }
}
struct Car {
let make: String
let model: String
let year: Int
let color: String
let nickname: String
var gasLevel: Double {
willSet {
precondition(newValue <= 1.0 && newValue >= 0.0,
"New value must be between 0 and ...Read now
Unlock full access