How to create 2d fixed-size array in Swift

How to create 2d fixed-size array in Swift

Recently I am doing some code tests and found that fixed-size array creation is not as simple in Swift compare to other languages, i.e.

1
2
3
4
5
6
// In C++

int array1[64];    // 1-dimension array size 64

int array2[64][64] // 2-dimension array size 64x64

After a bit digging, the way to do it in swift:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// in Swift:

var array1 = [Int?](repeating: nil, count: 64) // 1 dimension array

var array2 = [[Int?]](
 repeating: [Int?](repeating: nil, count: 64)
 count: 64
) // 2-dimension array size 64x64

// Access it just like normal

array2[4][2] = 42

print(array2[4][2]) // output: 42

So a 3d array would be:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// in C++

int array3[3][3][3];

// in Swift !!!!!!

var array3 = 
[[[Int?]]](
  repeating: [[Int?]](
    repeating: [Int?](
      repeating: nil,
      count: 3),
  count: 3),
count: 3)

Now you know how, you can try 7-dimension, happy coding 🍳

comments powered by Disqus