TypeCasting Doubt and SqlDelight dependency error

Hello,

I am new to Kotlin Multiplatform. I have two doubts regarding this language and platform.

  1. I have created commonMain.kt . In this file I have created a function which returns array of string. But when I try to run this framework on iOS, this function returns array of type “KotlinArray”. Now if I have to typecast this “KotlinArray” in to [String] or NSArray in swift. How should I proceed?
    //here is the code for this function
    fun stringArrReturn(): Array {
    val arr = arrayOf(“a”,“b”,“c”)
    return arr
    }

  2. it is regarding sqlDelight library…I wrote all possible code related to sqlDelight in common main.kt . I don’t get any error while compiling this.
    But When I try to run it on iOS and try to add in database I get this error -
    kotlin.Error, kotlin.RuntimeException and subclasses aren’t propagated from Kotlin to Objective-C/Swift.

//code for SqlDelight
@ThreadLocal
object CommonDatabase {
var driver: SqlDriver? = getDriver()
val database: Database by lazy {
println()
print(“getting driver”)
Database(driver!!)
}

private val observers: MutableMap<Int, Query.Listener> = mutableMapOf()

fun addTodo(title: String, completed: Boolean) {
    println()
    println("add todos... query")
    val completedNum = if (completed) 1L else 0L
    database.databaseQueries.insert(title, completedNum)
}

fun observeTodos(id: Int, onChangeCallback: (List<Todo>) -> Unit) {
    println()
    println("observe todos... query")
    if (observers.containsKey(id)) {
        throw RuntimeException("Already observing with id $id")
    } else {
        val listener = object : Query.Listener {
            override fun queryResultsChanged() {
                onChangeCallback(database.databaseQueries.selectAll().executeAsList())
            }
        }
        observers[id] = listener
        database.databaseQueries.selectAll().addListener(listener)
    }
}

fun stopObservingTodos(id: Int) {
    println("stop observing... query")
    observers[id]?.let {
        database.databaseQueries.selectAll().removeListener(it)
    }
}

fun deleteAll() {
    println("delete items")
    database.databaseQueries.deleteAll()
}

}

Thank you.

Hello, @callofdutybofan!

  1. You can use listOf instead. Here one can find an exact list of Kotlin-Swift concepts translation. This document states, that Kotlin analogue of the NSArray is the List.
  2. This issue looks slightly more complex. Please provide full stacktrace, and we’ll see what can be done.
1 Like

Thanks a lot for quick reply. Thank you for the link. In second query I was following this tutorial - Kotlin Multiplatform for Clean Architecture - inovex GmbH - it was issue from my side. I went through the tutorial once again and managed to resolve it.
Thank you.