April 2026
Intermediate
683 pages
25h 29m
English
In Chapter 9, as you were learning about class inheritance, we highlighted that class conformance can be checked using the is and as keywords. Because protocols also act like parent types, the same keywords can be used to check protocol conformance—and the syntax won’t change much either.
Listing 10.15 demonstrates the usage of the is keyword. if obj is Food will return true for Burger instances but false for Computer instances, because Burger implements the Food protocol.
protocol Food { var calories: Int { get } }struct Burger: Food { let calories = 300 }struct Computer { var brand: String var model: String}let objects: [Any] = [Burger(), Computer(brand: "Apple", model: "MacBook Pro")]for obj in
Read now
Unlock full access