Vsevolod Ganin
06/27/2022, 1:09 PMtry ... catch ...
in Kotlin while calling interface
method implemented in swift/objc? I saw relatively new ForeignException
but afaik it only works for cinteroped libraries with special flagSam
06/28/2022, 1:46 PMVsevolod Ganin
06/28/2022, 1:57 PMAn error pointer is passed into the function and if after calling the function, it has a value, then the function has generated an error.I’m getting the idea that Kotlin-ObjC interop layer could do this implicitly and rethrow as Kotlin exception if the error pointer has the value. Please correct me if I’m getting a wrong impression
Vsevolod Ganin
06/28/2022, 5:23 PMNSException
. For instance, you can take XCTest framework. It throws _XCTestCaseInterruptionException
when assertion fails as the means to stop execution of current test. If your test is written partly in Kotlin and uses XCTest assertions then the whole process just aborts instead of marking test failure and let other test execute.Sam
06/28/2022, 5:46 PMCorrado Quattrocchi
08/24/2023, 10:25 AMDefFileTask
appends foreignExceptionMode = objc-wrap
. Though when I run the tests the behaviour remains unchanged and the whole program crashes.
I can see that foreign exception is still supported on the master branch from kotlin/native and is not marked as deprecated, so I would expect it to work.
Here is some code:
// kotlin wrapper for the obj-c library
@Throws(ForeignException::class)
actual fun wrapperMethod(par1: String, par2: (par1_1: Par1_1, par2_2: Par2_2) -> Unit) {
MethodFromObjCLibrary(par1, par2) // inside here is where the NSException eventually is called. I tried to wrap it inside a try/catch, but didn't work.
}
// swift tests
wrapperMethod("Something") { par1_1, par2_2 in
// do something with XCTests
}
Now I’m not sure whether I understood how this flag should work and the documentation on that is very poor. Did you guys solved the issues you were having? If yes (or no), can you point a finger for me in the right direction? Thanks in advance!
For instance, you can take XCTest framework. It throwswhen assertion fails as the means to stop execution of current test._XCTestCaseInterruptionException