Swift 6 · Types and protocols

Protocols and extensions

A protocol describes requirements, and an extension adds default behavior.

protocol Describable {
    var name: String { get }
}

extension Describable {
    func describe() -> String { "I am \(name)" }
}

struct Dog: Describable { let name: String }

Official Swift 6 documentation

More types and protocols commands

All types and protocols commands · Swift 6 cheatsheet

Was this page helpful?