【问题标题】:'NSInvalidArgumentException', reason: '*** -getValue: only defined for abstract class. Define -[KotlinInt getValue:]!''NSInvalidArgumentException',原因:'*** -getValue:仅为抽象类定义。定义 -[KotlinInt getValue:]!'
【发布时间】:2019-10-30 07:49:32
【问题描述】:

我正在使用 Kotlin 多平台构建 iOS 框架,除了奇怪的问题之外,一切都运行良好。

来自 Kotlin 的代码:

fun example(): Map<Int, Double> {
        val result = mutableMapOf<Int, Double>()
        result[1] = 9.99
        return result
}

使用框架是Swift:

let example = example()

应用程序将崩溃并显示如下跟踪

2019-10-30 15:37:19.096474+0800 Test[73756:3057881] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -getValue: only defined for abstract class.  Define -[KotlinInt getValue:]!'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff23baa1ee __exceptionPreprocess + 350
    1   libobjc.A.dylib                     0x00007fff50864b20 objc_exception_throw + 48
    2   Foundation                          0x00007fff2584a6b3 _NSRequestConcreteObject + 0
    3   Foundation                          0x00007fff256bea7e -[NSNumber copyWithZone:] + 129
    4   CoreFoundation                      0x00007fff23add550 __NSSingleEntryDictionaryI_new + 128
    5   CoreFoundation                      0x00007fff23ba56f3 -[NSDictionary initWithDictionary:copyItems:] + 403
    6   CoreFoundation                      0x00007fff23c07fc7 -[__NSPlaceholderDictionary initWithDictionary:copyItems:] + 135
    7   libswiftFoundation.dylib            0x00007fff511ef3a0 $sSD10FoundationE26_forceBridgeFromObjectiveC_6resultySo12NSDictionaryC_SDyxq_GSgztFZ + 192
    8   libswiftFoundation.dylib            0x00007fff511e3593 $sSD10FoundationE36_unconditionallyBridgeFromObjectiveCySDyxq_GSo12NSDictionaryCSgFZ + 67
    9   Test                                0x0000000102f54320 $s4Test11ContentViewV3calSSyF + 1104
    10  Test                                0x0000000102f53e3b $s4Test11ContentViewV4bodyQrvg + 91
    11  Test                                0x0000000102f549c1 $s4Test11ContentViewV7SwiftUI0C0AadEP4body4BodyQzvgTW + 17
    12  SwiftUI                             0x00007fff2bfb1c49 $s7SwiftUI8ViewBody33_9F92ACD17B554E8AB7D29ABB1E796415LLV5applyy0D0QzxF + 585
    13  SwiftUI                             0x00007fff2bfb2df9 $s7SwiftUI8ViewBody33_9F92ACD17B554E8AB7D29ABB1E796415LLVyxG14AttributeGraph07UntypedM0AafGP7_update_5graph9attributeySv_So10AGGraphRefaSo11AGAttributeatFZTW + 137
    14  AttributeGraph                      0x00007fff2f8f1849 $sTA + 25
    15  AttributeGraph                      0x00007fff2f8d9255 _ZN2AG5Graph11UpdateStack6updateEv + 1111
    16  AttributeGraph                      0x00007fff2f8d9513 _ZN2AG5Graph16update_attributeEjb + 377
    17  AttributeGraph                      0x00007fff2f8de131 _ZN2AG8Subgraph6updateEj + 929
    18  SwiftUI                             0x00007fff2c10d100 $s7SwiftUI9ViewGraphC14runTransaction33_D63C4EB7F2B205694B6515509E76E98BLL2inySo10AGGraphRefa_tF + 224
    19  SwiftUI                             0x00007fff2c10d517 $s7SwiftUI9ViewGraphC13updateOutputs2atyAA4TimeV_tFSb5prefs_Sb9idealSizeAC0F0V7outputstSo10AGGraphRefaXEfU_ + 103
    20  SwiftUI                             0x00007fff2c10d1d1 $s7SwiftUI9ViewGraphC13updateOutputs2atyAA4TimeV_tF + 145
    21  SwiftUI                             0x00007fff2c4af579 $s7SwiftUI16ViewRendererHostPAAE6render8interval17updateDisplayListySd_SbtFyyXEfU_yyXEfU_ + 1001
    22  SwiftUI                             0x00007fff2c4aef8a $s7SwiftUI16ViewRendererHostPAAE6render8interval17updateDisplayListySd_SbtFyyXEfU_ + 634
    23  SwiftUI                             0x00007fff2c4a4274 $s7SwiftUI16ViewRendererHostPAAE6render8interval17updateDisplayListySd_SbtF + 436
    24  SwiftUI                             0x00007fff2c65a182 $s7SwiftUI14_UIHostingViewC14layoutSubviewsyyF + 226
    25  SwiftUI                             0x00007fff2c65a1a5 $s7SwiftUI14_UIHostingViewC14layoutSubviewsyyFTo + 21
    26  UIKitCore                           0x00007fff47a52ad5 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 2478
    27  QuartzCore                          0x00007fff2b06e91d -[CALayer layoutSublayers] + 255
    28  QuartzCore                          0x00007fff2b073323 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 517
    29  QuartzCore                          0x00007fff2b07fa7c _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 80
    30  QuartzCore                          0x00007fff2afc6e54 _ZN2CA7Context18commit_transactionEPNS_11TransactionEd + 324
    31  QuartzCore                          0x00007fff2affc32f _ZN2CA11Transaction6commitEv + 643
    32  UIKitCore                           0x00007fff475906cd __34-[UIApplication _firstCommitBlock]_block_invoke_2 + 81
    33  CoreFoundation                      0x00007fff23b0d09c __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
    34  CoreFoundation                      0x00007fff23b0c808 __CFRunLoopDoBlocks + 312
    35  CoreFoundation                      0x00007fff23b07694 __CFRunLoopRun + 1284
    36  CoreFoundation                      0x00007fff23b06e66 CFRunLoopRunSpecific + 438
    37  GraphicsServices                    0x00007fff38346bb0 GSEventRunModal + 65
    38  UIKitCore                           0x00007fff47578dd0 UIApplicationMain + 1621
    39  Test                                0x0000000102f526fb main + 75
    40  libdyld.dylib                       0x00007fff516ecd29 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

其实我是想实现返回Map的函数,但是我在Xocde的IDE中检查example()的类型是[KotlinInt : KotlinDouble]。我不知道如何解决这个问题才能在 Xcode 中正常使用这个函数,没有例外。

【问题讨论】:

  • 这个问题有什么进展吗?我面临同样的行为。

标签: swift exception kotlin multiplatform


【解决方案1】:

我以为 Kotlin 中没有对应于 iOS Swift 中的“字典”的结构类型“Map”。尝试将 Map 替换为 MutableMap 并解决此问题。

fun example(): MutableMap <Int, Double> {
        val result = mutableMapOf<Int, Double>()
        result[1] = 9.99
        return result
}

但是,您也可以在 Swift 中将类型转换为其他用途。例如,

let example = example() as! [Int64 : Double]

【讨论】:

    【解决方案2】:

    kotlin 中存在 Map,这里举个例子:

    val map = mutableMapOf(1 to 1.2, 2 to 1.2, 3 to 1.5)
    println(map)
    map[4] = 3.4
    println(map)
    

    输出:

    {1=1.2, 2=1.2, 3=1.5}
    {1=1.2, 2=1.2, 3=1.5, 4=3.4}
    

    【讨论】:

    • 是的,你是对的。但是我们正在讨论 kotlin 和 swift 之间的 Kotlin-Multiplatform。如果你想在 swift 中使用 Map(Dictionary),你应该在 kotlin 中使用 MutableMap。否则,您将像在原始帖子中一样遇到此问题。
    猜你喜欢
    • 1970-01-01
    • 2011-01-01
    • 2015-10-11
    • 2019-07-13
    • 2023-03-21
    • 1970-01-01
    • 2020-11-04
    • 2018-02-08
    • 1970-01-01
    相关资源
    最近更新 更多