'NSInvalidArgumentException', reason: '*** -getValue: only defined for abstract class. Define -[K



我正在使用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中正常使用这个函数。

我认为在iOS Swift中,Kotlin中没有对应于"Dictionary"的结构类型"Map"。尝试用MutableMap替换Map并解决此问题。

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

但是,您也可以在Swift中强制转换类型以供其他使用。例如,

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

地图存在于kotlin,这里有一个例子:

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}

最新更新