Hey, I am trying to use Swift `thows` with kotlin...
# ios
p
Hey, I am trying to use Swift
thows
with kotlin. Swift
Copy code
@objcMembers public class AESCryptoBridge: NSObject {
    public func test() throws {
        throw CryptoError.invalidData
    }
}
On Kotlin I have access to NSError but I have no idea how to use is. Is there any resource that will explain me how to work with?
Copy code
AESCryptoBridge().testAndReturnError("I have no idea how to use NSError Pointer here")
I found this code but alloc is deprecated if I am correct
Copy code
val errorPointer: CPointer<ObjCObjectVar<NSError?>> = alloc<ObjCObjectVar<NSError?>>().ptr
Update: I found the solution 🙂
Copy code
val error = memScoped {
    allocPointerTo<ObjCObjectVar<NSError?>>()
}
AESCryptoBridge().testAndReturnError(error.value)
l
Note that the code in the update has a memory error, since the pointer is freed after the memScoped exits. You should set the 'error' variable inside the memScoped, and keep all code that uses it inside. If you don't, the allocator can choose to allocate something else over where 'error' was originally allocated, since it's freed.
p
ohh good to know! so I should do something like this
Copy code
memScoped {
    val error = allocPointerTo<ObjCObjectVar<NSError?>>()

    SwiftCryptoBridge().initializeMACKeyWithKeyAlias(keyAlias.alias, error.value)

    if (error.value != null) {
        throw IllegalStateException("initializeMACKey Failed for $keyAlias with error: $error")
    } else {
        print("initializeMACKey Successful for: $keyAlias")
    }
}
l
Yes. Now the error is allocated for the full time you're using it.
p
Hey, I am writing tests for my logic and I found behaviour I didn't expect. This is my Swift function
Copy code
public func verifyChain(derCertificates: [Data]) throws {
    print("<-- Before Swift Throws")
    throw CertificateManagerError.invalidCertificateData
    print("<-- After Swift Throws")
}
And here Kotlin
Copy code
actual override fun verifyChain(certs: List<Certificate>): Result<Unit> = memScoped {
            if (certs.isEmpty()) {
                return Result.failure(IllegalStateException("Certificate list is empty!"))
            }

            val dataCertificates = certs.map { (it.asPlatformCert() as ByteArray).toNSData() }

            val error = allocPointerTo<ObjCObjectVar<NSError?>>()
            Log.d("tag", "Enter Swift")
                certificateManager.verifyChainWithDerCertificates(
                    dataCertificates,
                    error.value,
                )
            Log.d("tag", "Leaved Swift")

            if (error.value != null) {
                return Result.failure(IllegalStateException("Verify Chain Failed with error: $error."))
            }

            Log.d("tag", "Error value before return ${error.value}")

        return Result.success(Unit)
    }
Somehow everything passed with this in my logs Debug: (🛠️) AGENT-tag: Enter Swift Debug: (🛠️) AGENT-tag: Leaved Swift Debug: (🛠️) AGENT-tag: Error value before return null <-- Before Swift Throws BUILD SUCCESSFUL in 10s Kotlin Code didn't waited for Swift to finish. so I didn't received NSError pointer when I needed. Do you have maybe idea how to fix it?