Protocols
A protocol lists requirements. Types adopt it and can add default behaviour in extensions.
Example
protocol Shape { func area() -> Double }
extension Shape { func describe() -> String { "area \(area())" } }
struct Sq: Shape { var s: Double; func area() -> Double { s * s } }
print(Sq(s: 3).describe())Exercise
Adopt a protocol.
struct A: {}
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.