Deniz Tumer
02/22/2024, 8:45 PMrusshwolf
02/22/2024, 8:55 PM@Throws
annotation to the relevant methods, and they'll be exported as throws
so you can try/catch from Swift.Deniz Tumer
02/22/2024, 8:56 PM@Throws(Throwable::class)
but our app is crashing inside of the try/catch block. Seems like the NSException isn't being captured as an exception in Kotlin?russhwolf
02/22/2024, 8:58 PMDeniz Tumer
02/22/2024, 8:59 PMextraOpts = listOf("-Xforeign-exception-mode", "objc-wrap")
Jeff Lockhart
02/22/2024, 8:59 PMNSException
isn't possible to catch in Kotlin. It's also not possible in Swift. NSError
can be propagated though. NSError
is more commonly used for non-fatal errors in Objective-C, which can be handled in Kotlin.Deniz Tumer
02/22/2024, 9:02 PM@Throws
?Deniz Tumer
02/22/2024, 9:07 PMextraOpts = listOf("-Xforeign-exception-mode", "objc-wrap")
This allows me to do the following in kotlin iosMain module:
try {
// some code that throws NSException
} catch (e: ForeignException) {
// Catch!
}
Deniz Tumer
02/22/2024, 9:07 PMJeff Lockhart
02/22/2024, 9:07 PM@Throws
will create an Objective-C header with an NSError
parameter. But you're working in the opposite direction, needing to handle an Objective-C error in Kotlin. The typical pattern for this is for the Objective-C code to similarly allow for passing an NSError
reference to propagate any error with.Deniz Tumer
02/22/2024, 9:09 PMJeff Lockhart
02/22/2024, 9:21 PMobjc-wrap
opt-in works around this for your use case here. It'd be good to communicate this on YouTrack so JetBrains is aware of your experience.Jeff Lockhart
02/22/2024, 9:24 PMNSException
throwing code to handle as NSError
in Swift somehow. Otherwise this would similarly be an issue using the APIs from Swift.Jeff Lockhart
02/22/2024, 9:27 PMNote that the opposite reversed translation is not implemented yet: Swift/Objective-C error-throwing methods aren't imported to Kotlin as exception-throwing.To adapt Objective-C
NSError
producing code, for APIs that handle errors this way, I created this useful wrapper that converts NSError
results into a Kotlin Exception
and throws. Example usage.Jiri Bruchanov
02/22/2024, 9:47 PMextraOpts = listOf("-Xforeign-exception-mode", "objc-wrap")
?
if it's crashing from ios code directly, not from cocoapod 3rd party libDeniz Tumer
02/22/2024, 10:38 PMpod("FirebaseAuth") {
version = libs.versions.cocoapods.firebase.get()
extraOpts = listOf("-Xforeign-exception-mode", "objc-wrap")
}
Jiri Bruchanov
02/22/2024, 10:42 PMDeniz Tumer
02/26/2024, 7:25 PMDeniz Tumer
02/26/2024, 7:26 PMJiri Bruchanov
02/26/2024, 7:28 PMDeniz Tumer
02/26/2024, 8:50 PMiosMain
module to capture this? I'm not experienced enough with this yet to know where it goes. Are there compiler options we can use when setting up a kotlin source set?Jiri Bruchanov
02/26/2024, 9:08 PMobjc_begin_catch
and objc_end_catch
functions could be somehow useful,
but I'm terrible with the cinterop, so not sure what, how to create argument for the begin_catch