0Pricing
Swift Academy · Lesson

2D Arrays and Nested Collections

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

2D Arrays and Nested Collections is a free Swift Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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.

Frequently asked questions

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

Yes — the full text of “2D Arrays and Nested Collections” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.

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; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

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