Ad space (banner)
๐ŸฆSwift Lessons
Lesson 24 / 68

Two-Dimensional Arrays (Tabular Data)

In this lesson you will learn how to use two-dimensional arrays in Swift so you can work with data laid out as a table. This is for anyone searching for "Swift 2D array how to use".

Putting arrays inside an array gives you a grid with rows and columns. This is called a two-dimensional array, and you access it with two indexes, as in grid[row][column].

The sample code declares two rows of three columns with let grid = [[1, 2, 3], [4, 5, 6]] and reads a value with two sets of square brackets, as in grid[1][2]. Notice how for row in grid takes each row in turn.

A common stumbling block for beginners is how the data is actually laid out in memory. It looks like a table, but knowing that it is really stored in a single line deepens your understanding.

In real-world development this idea applies whenever you handle grid-shaped data, from a game board with coordinates to spreadsheet-like data.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Swift directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)