If I have a function that takes a `vararg String` ...
# multiplatform
e
If I have a function that takes a
vararg String
is there any way to have that use a variadic function instead of
KotlinArray
on iOS? I imagine no because the vararg is an array, but I'm wondering if anyone has any ideas. My iOS team doesn't want to use
KotlinArray
so I'm providing a
Set<String>
overload for them, but it's not ideal.
j
You can write a Swift extension that takes a variadic parameter, converts it to a
KotlinArray
and calls the Kotlin function. I've done this in a few places to smooth out the Swift API with extension functions.
I've also done this with Swift
static func
extensions which call
KotlinClass.companion
, until something similar to
@JvmStatic
for native is available.
This is my
Swift.Array.toKotlinArray()
extension used in these other variadic extensions:
Copy code
extension Swift.Array where Element: AnyObject {

    func toKotlinArray() -> KotlinArray<Element> {
        KotlinArray<Element>(size: Int32(count)) {
            self[$0.intValue]
        }
    }
}
124 Views