Hi! I'm trying to add an extension to ByteArray th...
# multiplatform
j
Hi! I'm trying to add an extension to ByteArray that converts it to NSData, following this snippet: https://gist.github.com/noahsark769/61cfb7a8b7231e2069a9dab94cf74a62. I've added a new file with the extension in the iosMain module, but I cannot see it in the iOS code. What am a I missing?
m
Extension methods on Objective-C classes are not translated to extension methods in Objective-C or Swift. Only extension methods on Kotlin classes. Instead the method becomes as static method, like it would be from Java.
j
Hi @mkrussel, the extension is on the Kotlin class ByteArray:
public fun ByteArray.toData(): NSData = memScoped {
NSData.create(bytes = allocArrayOf(this@toData),
length = this@toData.size.toULong())
}
But for some reason I am not able to see this method in the generated Obj-C code
m
I'm going to guess that ByteArray is mapped to some Objective-C class, but I don't see what it is in the interop documentation. You could look at the header file that gets generated, and try to find the function in there and see how it got mapped.
j
A custom class is generated to represent the ByteArray type in the Obj-C header, but I do not see my extension method in there. I was wondering if maybe I need to add some other declaration in the common module for it to be visible
Now I'm trying to read the bytes directly like this:
*var* bytes = [UInt8]()
*let* it = byteArray.iterator()
*while* (it.hasNext()) {
      
bytes.append(UInt8(it.nextByte()))
    
}
Getting an exception saying that
hasNext
is abstract? However if I print the type of the iterator in the Xcode console I see
kotlin.ByteIteratorImpl
FYI, very stupid problem on my side... I was not seeing the changes on the iOS client because I was making them in a different repository... feel very dumb right now!!