iOS ● ● ○ Swift 6

Kodebits Day 65: Custom Subscript

Jul 27 2026
Practice subscripts with a short swift challenge.

What is the output?

struct Grid {
  var data = [1, 2, 3, 4]
  subscript(i: Int) -> Int {
    return data[i] * 10
  }
}
let grid = Grid()
print(grid[1])
print(grid[3])


Try it in the online Swift Playground →

[spoiler title="Solution"]

Answer:

20
40

Explanation:

Subscripts provide custom bracket-notation access with computed values.

[/spoiler]


Further Reading