iOS ● ● ○ Swift 6

Kodebits Day 10: Protocol Extension

Apr 22 2026
Default behavior from protocol extensions keeps code small and reusable.

What does this print?

protocol Greet {
  var name: String { get }
}
extension Greet {
  func hi() -> String {
    "Hi, \(name)"
  }
}
struct Dev: Greet {
  let name: String
}
print(Dev(name: "Sam").hi())


Try it in the online Swift Playground →

[spoiler title="Solution"]

Answer:

Hi, Sam

Explanation:

Dev gets hi() from the protocol extension, which interpolates name.

[/spoiler]


Further Reading