ios - 将 Kotlin 接口(interface)的 Swift 实现传递给 Kotlin 方法时出错

标签 ios swift kotlin kotlin-native

我正在 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/

相关文章:

ios - Swift xpath 搜索返回字符串

swift - 如何修复每个中的 geocodeAddressString 闭包

android - Android Services 与 MVVM 中的 Activity 通信(Model View View Model)

java - 如何使用 Gradle 4.4 创建一个包含所有依赖项的 jar?

kotlin - Kotlin Cinterops native 库名称

ios - 确保两个计时器的执行逻辑不会同时运行

ios - 无法将 NSUserDefaults 中的数据检索到今天的扩展中(Swift)

objective-c - 项目不是为 iphone 模拟器构建的。

html - 编辑 HTML 文件,发送 HTML Email iOS

iOS - UITableViewCell, UIAlertController