I have a common function written in kotlin which r...
# multiplatform
c
I have a common function written in kotlin which returns a ByteArray, and I'd like to use this on iOS. I though that ByteArray should be a Data on iOS, but it seems like it's a KotlinByteArray. Any way to convert it to a Data? Or something else that is "recognized" by swift?
probably performances aren't that great, but right now on kotlin side I'm using
myByteArray.toList()
in order to return a List<Byte> to the swift side, then on swift I'm casting every KotlinByte to a Int8 (
myKotlinByte as! Int8
) then every Int8 to UInt8 (
UInt8(bitPattern: myInt8)
) and finally merged every UInt8 to an array and pushed it to a Data object
n
Looks like a lot of work to me! Why not just push it byte by byte to NSMutableData?
Just checked what I’m doing in my case:
Copy code
NSArray<KotlinUByte *> *encodedRecord = [self encode];
    NSUInteger byteCount = encodedRecord.count;
    unsigned char *buffer = malloc(byteCount);
    for (NSUInteger i = 0; i < byteCount; i++)
        buffer[i] = [encodedRecord[i] unsignedCharValue];
    return [NSData dataWithBytesNoCopy:buffer length:byteCount];
Hope that helps.