Two-dimensional array in Swift
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
A two-dimensional array in Swift is an array of arrays — [[T]]. Swift does not have a built-in 2D array type, so you create one by nesting arrays. The outer array represents rows and each inner array represents a column. Initialization uses Array(repeating:count:) nested two levels deep, and access uses double subscript notation like grid[row][col]. For performance-critical code, consider a flat array with manual row/column indexing.
Creating a 2D Array
Literal Initialization
Using repeating:count:
Empty 2D Array
Type-Annotated
Accessing and Modifying Elements
Iterating Over a 2D Array
Common Operations
Transpose
Search
Flatten
Jagged Arrays (Uneven Rows)
Swift's [[T]] supports rows of different lengths:
Performance-Optimized Flat Array
For large matrices, a flat array with manual indexing avoids the overhead of nested arrays:
This uses contiguous memory, which is significantly faster for large matrices due to better cache locality.
Common Pitfalls
Array(repeating:count:)shares references for reference types:Array(repeating: [Int](), count: 3)creates three independent arrays because[Int]is a value type. But for reference types (classes), all rows would share the same instance. Use a loop or map to create independent instances.- Out-of-bounds access: Swift arrays crash on out-of-bounds access. Always check
row < grid.count && col < grid[row].countbefore accessinggrid[row][col], especially with jagged arrays. - Modifying during iteration: Modifying
grid[row][col]while iterating withfor row in gridcreates a copy of the row. Use index-based iteration (for i in 0..<grid.count) if you need to modify in place. - Assuming uniform row lengths: Unlike C or Java fixed-size arrays, Swift's
[[T]]does not enforce that all inner arrays have the same length. Agrid.append([1, 2])followed bygrid.append([3])is valid but makes column-based access unsafe. - Performance of nested arrays: Each inner array is a separate heap allocation. For large matrices (1000x1000+), use a flat
[T]with manualrow * cols + colindexing for better cache performance and fewer allocations.
Summary
- Create 2D arrays with
[[T]]syntax orArray(repeating: Array(repeating: value, count: cols), count: rows) - Access elements with double subscript
grid[row][col] - Use
enumerated()for indexed iteration andflatMapfor flattening - Swift does not enforce uniform row lengths — all inner arrays can have different sizes
- For large matrices, use a flat array with
row * cols + colindexing for better performance
Related reading
- Two-way / bidirectional Dictionary in C?
- Two elements in array whose xor is maximum
- Type List vs type ArrayList in Java
- type mismatch error, expected type LIST for querying a one-to-many relationship in AppSync
- type A requires that type B be a class type swift 4
- Type of expression is ambiguous without more context Swift
- TypeError only integer scalar arrays can be converted to a scalar index with 1D numpy indices array
- TypeError unhashable type 'dict

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.