【问题标题】:Using Swift protocol default implementation in Kotlin Multiplatform在 Kotlin Multiplatform 中使用 Swift 协议默认实现
【发布时间】:2021-04-30 09:20:51
【问题描述】:

我尝试在 Kotlin Multiplatform XCFramework 中使用 Swift 代码。

我有一个扩展了该协议的默认实现的协议

@objc protocol Greeting {
    var something: String { get }
}

extension Greeting {
    var something: String {
        return "Hello from Swift"
    }
}

我在 Platform.kt 中编写

class GreetingImpl: NSObject(), GreetingProtocol {

    override fun something(): String {
        return (this as GreetingProtocol).something() 
    }
}

actual class Platform actual constructor() {
    val object = GreetingImpl()
    val value = object.something() //Application builds but falls here
}

如何在 Kotlin Multiplatform 中使用 Swift 协议默认实现?

【问题讨论】:

    标签: swift kotlin kotlin-multiplatform xcframework


    【解决方案1】:

    据我所知,主要有两个问题:

    1. 扩展缺少@objc 注释。虽然这是 Swift 方面的限制,但这会阻止 Kotlin 提供完整的互操作性(Kotlin/Native 不支持与 Swift 的直接互操作性,只能通过 Objective-C [docs])。
    2. Objective-C 不支持协议默认实现(参见this 相关的 StackOverflow 问题)。

    所以,我想说在 Kotlin Multiplatform 中没有使用 Swift 协议默认实现的选项。

    【讨论】:

      猜你喜欢
      • 2021-01-19
      • 1970-01-01
      • 1970-01-01
      • 2020-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多