Hello,
I am new to Kotlin Multiplatform. I have two doubts regarding this language and platform.
-
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
} -
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.