Swift Academy · Lesson

2D Arrays and Nested Collections

Working with arrays of arrays, flattening with flatMap and compactMap.

Lesson 4 of 412 steps

2D Arrays and Nested Collections is a free Swift Academy lesson on CoddyKit. This is lesson 4 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 4 lessons in total.

Welcome

2D arrays and nested collections model grids, tables, graphs, and hierarchical data. Swift handles them naturally — just nest array types.

Declaring a 2D Array

```swift var grid: [[Int]] = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ] print(grid[1][2]) // 6 ```

Mutating Nested Elements

```swift var matrix = [[0,0,0],[0,0,0],[0,0,0]] matrix[1][1] = 1 // [[0,0,0],[0,1,0],[0,0,0]] ```

Creating a 2D Grid with map

```swift let rows = 3, cols = 4 let grid = (0..

Iterating a 2D Array

```swift for row in grid { for val in row { print(val, terminator: " ") } print() } ``` Or with indices when mutation is needed: ```swift for r in grid.indices { for c in grid[r].indices { ... } } ```

flatMap to Flatten

```swift let nested = [[1,2,3],[4,5],[6]] let flat = nested.flatMap { $0 } // [1,2,3,4,5,6] ``` Useful when you need to process all elements as a single sequence.

Jagged Arrays

Swift 2D arrays don't need uniform row lengths: ```swift let jagged: [[Int]] = [[1],[2,3],[4,5,6]] print(jagged[2].count) // 3 ``` Each row is an independent array.

Array of Dictionaries

```swift let users: [[String: Any]] = [ ["name": "Alice", "age": 30], ["name": "Bob", "age": 25] ] for user in users { print(user["name"] ?? "?") } ```

Array of Structs

Prefer typed structs over `[String: Any]`: ```swift struct User { let name: String; let age: Int } var users = [User(name:"Alice",age:30), User(name:"Bob",age:25)] let sorted = users.sorted { $0.age < $1.age } ```

Sparse Grids with Dictionary

For sparse data, use a dictionary instead of nested arrays: ```swift var sparse: [SIMD2: String] = [:] sparse[SIMD2(0,0)] = "start" sparse[SIMD2(9,9)] = "end" ``` O(1) lookup and no wasted memory for empty cells.

Quick Check

What does `nested.flatMap { $0 }` return for `[[1,2],[3,4]]`?

Recap

Key takeaways: • `[[T]]` — declare 2D arrays, access with `[row][col]` • `.flatMap { $0 }` — flatten nested arrays • Use structs over `[String: Any]` for type safety • Jagged arrays are fine — rows are independent • Use a `Dictionary` for sparse grids Course complete! Next: Dictionary CRUD operations.
Free to start

Learn Swift with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
122
Lessons
409

Frequently Asked Questions

Is the “2D Arrays and Nested Collections” lesson free?

Yes — any 3 lessons of the Swift Academy learning path, including “2D Arrays and Nested Collections”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Swift Academy course includes 4 lessons in total.

What will I learn in “2D Arrays and Nested Collections”?

Working with arrays of arrays, flattening with flatMap and compactMap. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Swift Academy?

No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.

How long does the “2D Arrays and Nested Collections” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Swift Academy lesson?

Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Creating and Mutating Arrays
  2. Sorting, Searching and Slicing
  3. Map, Filter and Reduce on Arrays
  4. 2D Arrays and Nested Collections
← Back to Swift Academy