https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
d

Diego

02/22/2019, 2:46 PM
Hello guys, I have function in common that returns
ByteArray
but on the iOS world it is converted to
KotlinByteArray
and not to
NSData
. In the Kotlin world I have the following extensions to convert
ByteArray
to
NSData
and visceversa.
Copy code
private fun ByteArray.toNSData(): NSData = memScoped {
    return NSData.create(
        bytes = toCValues().getPointer(this),
        length = size.toUInt()
    )
}

private fun NSData.toByteArray(): ByteArray = memScoped {
    val size = length.toInt()
    val nsData = ByteArray(size)
    memcpy(nsData.refTo(0), bytes, size.toUInt())
    return nsData
}
Does someone know how I can convert
KotlinByteArray
to
NSData
and visceversa in Swift? 🙂
r

ribesg

02/22/2019, 2:48 PM
Wait, why can’t you just call your Kotlin extension in Swift?
🙌 1
j

Jonas Bark

02/22/2019, 2:59 PM
you could do that, but they shouldn't be marked private then of course
👍 1
d

Diego

02/22/2019, 2:59 PM
I did not think about that LOL 😅
Thanks @ribesg! 🤜🤛
4 Views