Chapter 40: Swift Arrays: Multidimensional

1. What is a Multidimensional Array?

A multidimensional array is simply an array of arrays.

In other words:

  • You have an outer array
  • Each element of the outer array is itself an array
  • So you access elements using two (or more) indices

Real-life analogies:

  • 2D array → chessboard (8 rows × 8 columns)
  • 2D array → monthly calendar (weeks as rows, days as columns)
  • 2D array → spreadsheet table (rows and columns)
  • 3D array → stack of chessboards, or RGB image (height × width × color channels)

In Swift we usually talk about 2D arrays (most common), sometimes 3D, and rarely more.

2. Creating a 2D Array — all common ways

Way 1 – Literal syntax (most readable for small fixed data)

Swift

Way 2 – Empty 2D array with fixed rows & columns

Swift

Way 3 – From existing data (very common)

Swift

3. Reading from a 2D Array

Swift

Safe access (very important)

Swift

4. Modifying a 2D Array (only if var)

Swift

5. Looping over 2D Arrays — most common patterns

Pattern 1 – Nested for-in (classic & clear)

Swift

Output:

text

Pattern 2 – With row & column indices

Swift

Pattern 3 – Using indices

Swift

6. Real-Life Examples You Will Actually Write

Example 1 – Monthly sales table

Swift

Example 2 – Tic-tac-toe board

Swift

7. Very Common Beginner Mistakes & Correct Habits

Mistake Wrong / Dangerous code Correct / Safe habit Why?
Forgetting outer array can be empty matrix[0][0] without check guard !matrix.isEmpty else { … } Crash if no rows
Assuming all rows have same length matrix[row][col] without check if col < matrix[row].count { … } Rows can have different lengths
Force-unwrapping first/last of inner array matrix[0].first! matrix[0].first ?? default Inner array can be empty
Modifying let array let grid = …; grid[0][0] = 99 Use var if you need to change Immutable
Using fixed indices without validation matrix[5][5] Use indices.contains or safe subscript Out-of-bounds → crash

8. Quick Reference – Most Useful 2D Array Operations

Goal Recommended code Notes
Create empty 2D grid Array(repeating: Array(repeating: 0, count: cols), count: rows) Classic way
Safe element access matrix[safe: row]?[safe: col] ?? default With safe subscript extension
Loop with row & column indices for r in matrix.indices { for c in matrix[r].indices { … } } Most explicit & safe
Print nicely matrix.map { $0.map(String.init).joined(separator: ” “) }.joined(separator: “\n”) Quick debug print
Get row matrix[2] Returns Array (copy)
Get column (trickier) (0..<matrix.count).map { matrix[$0][col] } No built-in column access

9. Small Practice – Try these

  1. Create a 4×5 grid filled with zeros → Set position [1][2] = 99 → Print the grid row by row
  2. Create a 3×3 tic-tac-toe board → Place “X” in center → Place “O” in all four corners → Print the board with nice separators
  3. Create a sales table (3 products × 4 months) → Print it like a real table with headers

Paste your code here if you want feedback or cleaner versions!

What would you like to explore next about arrays?

  • Sorting 2D arrays (by row, by column, custom)
  • Transposing a matrix (rows ↔ columns)
  • Flattening 2D → 1D and vice versa
  • Multidimensional arrays in SwiftUI (grids, tables)
  • Arrays vs ArraySlice vs ContiguousArray
  • Or move to another topic (dictionaries, sets, optionals…)

Just tell me — we’ll keep going in the same clear, detailed, patient style 😊

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *