-
Re: index(of: Element) on 2d array’s outside array
QuinceyMorris Sep 13, 2016 10:46 PM (in response to macuser1984)It actually looks like a bug in the compiler. You should submit a bug report with the 1st code fragment.
However, if you just want to get hold of the current index, you can do it more easily like this:
let int2dArray = [[3, 2], [8, 4]] for (iCurrentIndex, i) in int2dArray.enumerated () { for j in i { print (iCurrentIndex, i) // etc } }
That "enumerated" method allows you to get the index and the element at the same time.
Edit: Submitted as bug 28294667.
-
Re: index(of: Element) on 2d array’s outside array
eskimo Sep 14, 2016 3:24 AM (in response to QuinceyMorris)That "enumerated" method allows you to get the index and the element at the same time.
Regardless of the following, this is definitely the right way to solve macuser1984’s problems (nice handle btw!). Everything below is just FYI.
index(of:)
won’t work because it requiresElement
to be equatable andint2dArray
’s elements are of type[Int]
.[Int]
values can be compared, they don’t conform toEquatable
.let a1 = [1, 2, 3] let a2 = [1, 2, 3] func isEquals<X>(_ x: X, _ y: X) -> Bool where X : Equatable { return x == y } print(a1 == a2) print(isEquals(a1[0], a2[0])) print(isEquals(a1, a2)) //^^^^^^^^^^^ Argument type '[Int]' does not conform to expected type 'Equatable'
For more background on this, look up “conditional conformance” in the Generics Manifesto.
Share and Enjoy
—
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardwarelet myEmail = "eskimo" + "1" + "@apple.com"
-
Re: index(of: Element) on 2d array’s outside array
QuinceyMorris Sep 14, 2016 9:46 AM (in response to eskimo)I revised my bug report to change it to a complaint about the misleading error message.
-
-
Re: index(of: Element) on 2d array’s outside array
macuser1984 Sep 15, 2016 7:47 AM (in response to QuinceyMorris)QuinceyMorris - I just tried this solution with my actual code (that's more complicated than the simple example I wrote here), and it now works as I expected it to. It actually cleaned up my code and made it easier to read!
eskimo - Thanks for the extra information, especially the example!
-