Hello, newbie to native world. What is the best wa...
# kotlin-native
a
Hello, newbie to native world. What is the best way to handle obj c functions that throws an exception. Basically they carry an extra param with a type
CPointer<ObjCObjectVar>NSError?>>
. Using try and catch is not useful and I don't want to pass in null
s
I have this little utility function I use
Copy code
import kotlinx.cinterop.CPointer
import kotlinx.cinterop.ObjCObjectVar
import kotlinx.cinterop.alloc
import kotlinx.cinterop.memScoped
import kotlinx.cinterop.ptr
import kotlinx.cinterop.value
import platform.Foundation.NSError
Copy code
class NSErrorException(val nsError: NSError) : Exception(nsError.localizedDescription)
Copy code
inline fun <T> throwNSErrors(block: (CPointer<ObjCObjectVar<NSError?>>) -> T): T = memScoped {
    val err = alloc<ObjCObjectVar<NSError?>>()
    val result = block(err.ptr)
    if (err.value != null) throw NSErrorException(err.value!!)
    result
}
a
Thank you @Sam
@Sam do you know how to observe properties from Kotlin?? Like we can do from Swift with
addObserver
s
Hmm... I'd need to see an example. I don't think I've ever used
addObserver
in Swift.
It is worth noting that Kotlin uses Objective-C as a go-between with Swift. So some Swift-only APIs/idioms aren't directly available to Kotlin.