#22: Protocols and Dynamic Dispatch
Question
// Swift (5.0) Quiz
— objc.io (@objcio) July 17, 2019
import Foundation
protocol A {
var desc: String { get }
}
extension A {
var desc: String { "A" }
}
struct S: A {
var street: String
var desc: String { street }
}
let a = S(street: "S")
a[keyPath: \A.desc]
// What's the output?
Solution
Solution: S
— objc.io (@objcio) July 18, 2019
Because `desc` is part of the protocol, it is dynamically dispatched.
This is the same regardless of whether we access `desc` the regular way, or through a key path. https://t.co/s7500CWmAg