将 Kotlin 接口的 Swift 实现传递给 Kotlin 方法时出错

Error when passing a Swift implementation of a Kotlin interface to a Kotlin method

我正在 iOS 上对 Kotlin Native 进行一些试验,我想尝试的一件事是 Swift 实现 Kotlin 定义的接口。但是,当我尝试将 Swift 对象传回 Kotlin 代码时,我最终崩溃了。

我正在使用 kotlin gradle 插件版本 1.2.30 和 kotlin 本机版本 0.6.1

下面是一个最小的例子。正在将 Kotlin 代码编译为名为 KotlinCommon 的框架,然后将其包含在 xcode 项目中。

DemoClass.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")
    }
}