我有一个Kotlin多平台项目,它使用iOS库在CoreBluetooth设备上执行蓝牙操作。我有一些问题,在获得外围断开的回调。仔细观察,我发现生成的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 */ }
因此,我只能覆盖这些方法中的一个。我的问题是我是不是漏掉了什么?就像一种覆盖这两种方法的方法。
发布于 2021-09-20 12:59:49
在kotlin中,您不能声明两个具有相同签名的函数,只有参数名称不同。但是在ObjC你可以。
要支持这种互操作,可以使用@Suppress("CONFLICTING_OVERLOADS")
,如文档中描述的这里。
要用冲突的Kotlin签名覆盖不同的方法,可以向类添加一个@ class (“CONFLICTING_OVERLOADS”)注释。
@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?
) {
}
https://stackoverflow.com/questions/69254560
复制相似问题