我正在 iOS 上使用 Kotlin Native 进行一些试验,我想尝试的一件事是使用 Swift 实现 Kotlin 定义的接口(interface)。但是,当我尝试将该 Swift 对象传回 Kotlin 代码时,我最终崩溃了。
我使用的是 kotlin gradle 插件版本 1.2.30 和 kotlin native 版本 0.6.1
下面是一个最小的例子。 Kotlin 代码被编译成名为 KotlinCommon 的框架,然后包含在 xcode 项目中。
演示类.kt
class DemoClass {
fun process(dependency: Dependency) {
dependency.foo()
}
}
interface Dependency {
fun foo()
}
SwiftDependency.swift
import KotlinCommon
class SwiftDependency : KotlinCommonDependency {
func foo() {
NSLog("hello")
}
}
然后,我尝试从我的 iOS UI 运行
let module = KotlinCommonDemoClass()
let dependency = SwiftDependency()
module.process(dependency: dependency)
第三行导致崩溃并出现以下错误:
*** NSForwarding: warning: object 0x101858d20 of class 'KotlinNativeDemo.SwiftDependency' does not implement methodSignatureForSelector: -- trouble ahead
Unrecognized selector -[KotlinNativeDemo.SwiftDependency toKotlin:]
Kotlin Native 不支持这个用例吗?还是我配置有误?
最佳答案
目前 SwiftDependency
是子类 NSObject
所必需的:
class SwiftDependency : NSObject, KotlinCommonDependency {
func foo() {
NSLog("hello")
}
}
关于ios - 将 Kotlin 接口(interface)的 Swift 实现传递给 Kotlin 方法时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49092085/