iOS ● ● ● Swift 6

Kodebits Day 13: Associated Types

Apr 27 2026
Associated types let protocols model flexible value containers.

What does this print?

protocol Box {
  associatedtype Item
  var item: Item { get }
}
struct IntBox: Box {
  let item: Int
}
let b = IntBox(item: 9)
print(b.item * 2)


Try it in the online Swift Playground →

[spoiler title="Solution"]

Answer:

18

Explanation:

The Box protocol uses an associated type Item as a placeholder. IntBox conforms by setting Item = Int, making item a concrete Int property.

[/spoiler]


Further Reading