how to access Array from Kotlin Mobile Platform to iOS or Android?

Viewed 1431

I am trying to create an bubble sort in KMM app. Now KMM func takes param from native side and communicate it with KMM side of app, and should return back the sorted array back to native side. Now, there are few doubts in my mind.

  1. Am I declaring kotlinArray in swift correctly? If I try specifying it as normal swift Array it just cannot cast to Kotlin array and gives me casting error. So this is something that I came up with. So is this correct way to communicate with KotlinArray from swift?
  2. fun bubbleSort works perfect when I am trying to run it on native project. But when I am trying to run KMM project it's just giving me an garbage value. So can anyone please help me understand how this can be achived?

Following is my source code.

SourceCode for ContentView.swift:

func greet() -> String {
    
    let array = KotlinIntArray(size: 5)
    array.set(index: 0, value: 2)
    array.set(index: 1, value: 15)
    array.set(index: 2, value: 1)
    array.set(index: 3, value: 8)
    array.set(index: 4, value: 4)
    let sortedArray = Greeting().bubbleSort(arr:array)

    print("Entered Array :\(array)"). //output: Entered Array :kotlin.IntArray@187bd58

    print("Sorted Array :\(sortedArray)") //Output: Sorted Array :kotlin.IntArray@187bd58
    
    return Greeting().greeting()
    
}
SourceCode for Greetings.kt:
package com.example.kmm_perofomance.shared


class Greeting {
    fun greeting(): String {
        return "Hello, ${Platform().platform}!"
    }

     fun bubbleSort(arr:IntArray):IntArray{
         for (i in 1..arr.count()) {
           for (j in 1..arr.count() - i) {
               if( arr[j] < arr[j-1]) {
                   var temp = arr[j-1]
                   arr[j-1] = arr[j]
                   arr[j] = temp
               }
           }
       }
        return arr
    }
}

Output screenshot: Output screenshot:

Any help would be much appreciated.

2 Answers

The "garbage value" is the default toString for that type, or maybe a wrapper type of the array. In any case, what that says is "kotlin.IntArray", which is the type, and "@_____", which is either the id or pointer value (not sure). In any case, it's not the list of values, obviously. To print that, you should write a function that prints that for you.

KMM converts List and Swift arrays back and forth. Kotlin arrays are not transformed and are simple object types that Swift can interact with, so you example above seems fine. It's just that you're not printing it properly.

I have no idea if your sorting code is correct, though.

I would point out that you are modifying the array you pass in and returning it, which will result in your output to be incorrect. At the point you're printing array and sortedArray, they'll be the same value. They'll both be sorted.

I would suggest you to use ArrayList<Int> rather then using KotlinIntArray. Writing ArrayList<Int> will by default convert it to NSMutableArray in swift.

Example :-

You need to have this in you shared module :-

fun sortArray(array: ArrayList<Int>) : ArrayList<Int> {
    // Your Swap logic here
    return array
}

Then in your swift code you can have this :-

let array = NSMutableArray()
    array.add(2)
    array.add(5)
    array.add(1)
    array.add(7)
    print("\(Greeting().sortArray(array: array))")

Your Output will be like this :-

(2, 5, 1, 7)

From this you won't need to write seperate function for getting array element one by one.

Related