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]