Reputation: 4589
I'm wondering..is there a way to easily acces a value which is stored in an multidimensional array. I provided an example:
var arrayOfTwoArrays = [[2,3,4], [5,6,7]]
arrayOfTwoArrays[0,2] //error saying: Extra argument in call
arrayOfTwoArrays[0,2] should return (if this worked) value 4. This is not happening and I'm getting an error. The Apple documentation models multidimensional array as a linear array, but I don't want the extra work.
Upvotes: 1
Views: 73
Reputation: 40973
An array of arrays is not quite the same thing as a multidimensional array (for example, the inner arrays can be of different sizes). So to get values from the inner array, first fetch it, and then subscript it:
arrayOfTwoArrays[0][2]
Incidentally, if your intent was to get the last element of the first array, and the size of the arrays can vary and sometimes be empty, you could write it like this:
if let x = arrayOfTwoArrays.first?.last {
// use x
}
which would account for the possibility of empty arrays. You could then add an else
to handle errors, or ??
to provide defaults, if this is a possibility you want specific handling for.
Upvotes: 1