Swift 6 · Types and protocols

Enums with associated values

Each case can carry its own data, and switch extracts it.

enum Result {
    case success(Int)
    case failure(String)
}

switch result {
case .success(let value): print(value)
case .failure(let message): print(message)
}

Official Swift 6 documentation

More types and protocols commands

All types and protocols commands · Swift 6 cheatsheet

Was this page helpful?