Kendel
Kendel

Reputation: 1708

How to create a 3D array/matrix in swift?

I am trying to create a 3D array which will store an image's height, width, and RGB values as the dimensions of matrix/array. Here is what I am trying:

var array = Array<Array<Array<Int>>>()
var testArray = [[[Int]]] ()

Now that I have initialize these arrays, how would I set a specific number of rows and columns? Thanks!

Upvotes: 4

Views: 3580

Answers (1)

PixelCloudSt
PixelCloudSt

Reputation: 2805

Until fixed length arrays are supported in Swift, I'd do it something like this:

let zArry = [Int](repeating: 0, count: 10)
let yArry = [[Int]](repeating: zArry, count: 20)
let xArry = [[[Int]]](repeating: yArry, count: 30)

let val = xArry[2][1][1]

Upvotes: 6

Related Questions