I want to use SwiftUI `ForEach` on an `KotlinArra...
# multiplatform
s
I want to use SwiftUI
ForEach
on an
KotlinArray
, but I get
Generic struct 'ForEach' requires that 'KotlinArray<MyType>' conform to 'RandomAccessCollection'
. Is there any compatiblity layer build in for that?
l
Can't you make it conform to that
RandomAccessCollection
protocol via a Swift extension?
s
Not sure how. 🤷‍♂️ As a workaround I now will convert it "toList()" and write that into a variable.
l
Search how to implement a protocol via a Swift extension on Google and I'm pretty sure you'll find out. Converting to a list is fine as well.
s
Make a swift file and in that file add something like the following
extension KotlinArray<T>: RandomAccessCollection {}
That should get you started. It's possible that you won't be able to do that with a generic T and will need to specify your class directly. Generics and protocols can be finicky.
🙏 1
a
Thanks for that tidbit @Sam! I'm curious if anyone has put together a collection of these kinds of utilities to make Kotlin/Swift ergonomics a bit better… it might be tricky to package and redistribute though