Hi all ! Beginner question here, let’s say I defin...
# kotlin-native
b
Hi all ! Beginner question here, let’s say I define an extension in
iosMain
:
Copy code
fun NSData.toByteArray(): ByteArray = ByteArray(this@toByteArray.length.toInt()).apply {
    usePinned {
        memcpy(it.addressOf(0), this@toByteArray.bytes, this@toByteArray.length)
    }
}
This one allows to convert a
NSData
in Objective-C to a
ByteArray
in Kotlin Is it possible to use this function directly in XCode on a
NSData
object ?
m
Kotlin extensions on Objective-C classes and protocols are not imported to Swift and Objective-C I believe the rational was that you get runtime crashes if there are duplicate extensions, and they cannot tell what extension might get added in Objective-C or Swift. https://kotlinlang.org/docs/native-objc-interop.html#extensions-and-category-members
r
I think you could still call it by`FilenameKt.toByteArray(nsData)` from Swift. That should fall under "Kotlin extensions to other types are treated as top-level declarations with an additional receiver parameter" in that doc
🎉 1
b
@mkrussel @russhwolf thanks for your answers ! I can confirm it works when using the filename, so cool ! 😀