I have a 2d array and am trying to access the index of one of the elements of the outside array (therefore the index for a contained 1d array) as shown in the code below:
let int2dArray = [[3, 2], [8, 4]]
for i in int2dArray {
for j in i {
let iCurrentIndex = int2dArray.index(of: i)
}
}
When I try to use index(of: Element), I get the following error message: Cannot invoke 'index' with an argument list of type '(of: Array<Int>)’. I also notice in the code completion suggestions that index(of:) doesn’t show up, but index(where: (Element) -> Bool) does show up. Closures are still a relatively new concept to me (I’m not a total newbie to iOS development but not as experienced yet), so I’m not sure exactly what this method would take as a parameter. Why is this? Does any of this have to do with the element's conformance with Equatable (which is required for this method)?
In Swift 2.x and 1.x (and C++), I could use a C-style for loop and accomplish this because i (in this case) was the actual array index, like so:
for var i = 0; i < int2dArray.count; i++ {
for var j = 0; j < int2dArray[i].count; j++ {
let iCurrentIndex = i
}
}
Any suggestions?
Thanks!