【问题标题】:Reading Character Array from Shared class in KotlinMultiPlatform (KMM)从 Kotlin MultiPlatform (KMM) 中的共享类中读取字符数组
【发布时间】:2021-11-25 02:06:51
【问题描述】:

如何从 Kotlin 模块中正确读取 Swift 中可变数组的数据? Kotlin 共享模块:

data class Tape(private val capacity: Int) {
        val reel: MutableList<Char>
        ...
}

斯威夫特:

tape.reel.compactMap({ $0 as? Character } // leads to nothing in the array

【问题讨论】:

  • Char 在 Kotlin 中是一个“16 位 Unicode 字符”,所以严格来说 Swift 中的等价物是 UInt16,但你甚至可以从 Int 开始。
  • 这行不通,因为Character 是一个 swift 结构,所以 kotlin native 无法生成它。您可以使用type(of: $0) 检查变量的类型:在我的情况下,我得到了一些Shared_kobjcc0。这显然不是我所期望的:它必须是NSNumber 的某个子类,因为您不能将常规的Int 放在NSArray 中。我建议您在 JetBrains 问题跟踪器上create a problem
  • 现在您可以将其存储为整数
  • @PhilipDukhov,是的,我认为这将是我现在的工作。感谢您的帮助

标签: swift kotlin nsmutablearray kotlin-multiplatform kmm


【解决方案1】:

Kotlin Char 将被映射为UInt16

一种选择可能是将您的 char 列表映射到 Kotlin 中的字符串:

val reel: String

然后在Swift 方面使用它作为String

【讨论】:

    猜你喜欢
    • 2021-06-10
    • 1970-01-01
    • 1970-01-01
    • 2019-11-07
    • 1970-01-01
    • 2022-10-13
    • 1970-01-01
    • 2021-03-29
    • 1970-01-01
    相关资源
    最近更新 更多