Using Tuples in Swift

Level 279 keystrokes

Creates a tuple with named elements and reads them both by name (point.x) and by index (point.0).

Swift code

// A tuple with named elements x and y
1let point = (x: 10, y: 20)
// Read elements by name: x: 10, y: 20
2print("x: \(point.x), y: \(point.y)")
// Index 0 is the first element, same as point.x: 10
3print(point.0)

Output

x: 10, y: 20
10
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this Swift code and how often they appear.

  • (× 5
  • )× 5
  • :× 4
  • .× 3
  • ,× 2
  • "× 2
  • \× 2
  • =× 1

More Swift examples at level 2

← Swift typing practice