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.
- 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?
fun bubbleSortworks 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
}
}
Any help would be much appreciated.
