Kotlin/Native 与 Swift 的互操作性:生成的接口具有两个具有相同签名的方法

Kotlin/Native interoperability with Swift: generated interface with two methods having same signature

我有一个 Kotlin 多平台项目,它使用 CoreBluetooth 库在 iOS 设备上执行蓝牙操作。我在获取外围设备断开连接回调时遇到了一些问题。仔细检查后,我发现生成的 CBCentralManagerDelegateProtocol 接口(我重写)有两个具有相同签名的方法。

@kotlin.commonizer.ObjCCallable public open expect fun centralManager(central: platform.CoreBluetooth.CBCentralManager, didFailToConnectPeripheral: platform.CoreBluetooth.CBPeripheral, error: platform.Foundation.NSError?): kotlin.Unit { /* compiled code */ }

@kotlin.commonizer.ObjCCallable public open expect fun centralManager(central: platform.CoreBluetooth.CBCentralManager, didDisconnectPeripheral: platform.CoreBluetooth.CBPeripheral, error: platform.Foundation.NSError?): kotlin.Unit { /* compiled code */ }

因此,我只能重写其中一种方法。我的问题是我在这里遗漏了什么吗?就像一种覆盖这两种方法的方法。

在 kotlin 中,您不能声明两个具有相同签名、仅参数名称不同的函数。但是在 ObjC 中你可以。

要支持此互操作,您可以使用 @Suppress("CONFLICTING_OVERLOADS"),如文档中 here 所述。

To override different methods with clashing Kotlin signatures, you can add a @Suppress("CONFLICTING_OVERLOADS") annotation to the class.

@Suppress("CONFLICTING_OVERLOADS", "PARAMETER_NAME_CHANGED_ON_OVERRIDE")
override fun centralManager(
    central: CBCentralManager,
    didFailToConnectPeripheral: CBPeripheral,
    error: NSError?
) {
}

@Suppress("CONFLICTING_OVERLOADS", "PARAMETER_NAME_CHANGED_ON_OVERRIDE")
override fun centralManager(
    central: CBCentralManager,
    didDisconnectPeripheral: CBPeripheral,
    error: NSError?
) {
}