Could not cast value of type Swift._SwiftDeferredNSDictionary<Swift.String, Any> to KotlinMutableDictionary

404 Views Asked by At

I want to convert swift dictionary [String : Any] to KotlinMutableDictionary<NSString, NSString>. It doesn't allow to init with my swift dictionary.

    let kotlinDic = MykotlinClass.init(pages: <#T##KotlinMutableDictionary<NSString, NSString>#>)
// The class definitation
    
    let kotlinDic = MykotlinClass.init(pages: editableFiedlDic as! KotlinMutableDictionary<NSString, NSString>) 
// This is how I initialized
1

There are 1 best solutions below

0
On

You cannot cast them directly (see note), but you have to do something like that:

let swiftDic: [String : String] = [ "key1": "value1" , "key2": "value2" ]
let kotlinDic = KotlinMutableDictionary<NSString, NSString>.init(dictionary: swiftDic)

So in your case you should do

MykotlinClass.init(
    pages: KotlinMutableDictionary<NSString, NSString>.init(dictionary: editableFiedlDic)
)