You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

KMM技术问题:如何从Kotlin MutableMap获取绑定Swift Array?

解决Kotlin MutableMap转Swift绑定Array的类型不匹配问题

已成功将Kotlin MutableList绑定为Swift Array,但无法从Kotlin MutableMap<String, MutableList<ScreenIdentifier>>中按指定键获取对应的Swift绑定Array,以下是具体场景及解决方案:


成功案例:Kotlin MutableList转绑定Swift Array

Kotlin定义

var path : MutableList<ScreenIdentifier> = mutableListOf()

Swift扩展实现

extension Binding where Value == NSMutableArray {
    public func cast() -> Binding<[ScreenIdentifier]> {
        return Binding<[ScreenIdentifier]>(
            get:{ self.wrappedValue as NSArray as! [ScreenIdentifier] },
            set: { self.wrappedValue = NSMutableArray(array: $0) }
        )
    }
}

使用方式

NavigationStack(path: $path.cast()) {
...
}

失败场景:Kotlin MutableMap转绑定Swift Array

Kotlin定义

var paths : MutableMap<String, MutableList<ScreenIdentifier>> = mutableMapOf()

原Swift扩展实现(报错)

extension Binding where Value == NSMutableDictionary {
    public func getPath(level1URI: String) -> Binding<[ScreenIdentifier]> {
        return Binding<[ScreenIdentifier]>(
            get:{
                let dict = self.wrappedValue as NSDictionary as! [String:[ScreenIdentifier]]
                return dict[level1URI] as! NSMutableArray as NSArray as! [ScreenIdentifier]
            },
            set: {
                var modifiedDict = self.wrappedValue as! [NSString:NSMutableArray]
                modifiedDict[level1URI as NSString] = NSMutableArray(array: $0)
                self.wrappedValue = NSMutableDictionary(dictionary: modifiedDict as! KotlinMutableDictionary<NSString,NSMutableArray>)
            }
        )
    }
}

使用方式

NavigationStack(path: $paths.getPath(level1URI: myString)) {
...
}

报错信息(中文翻译)

调用Binding的实例方法getPath(level1URI:)要求KotlinMutableDictionary<NSString, NSMutableArray>和NSMutableDictionary类型等价


解决方案

报错核心原因是Kotlin的MutableMap在Swift中对应的类型是KotlinMutableDictionary,而非NSMutableDictionary,原扩展的约束类型写错了。修改后的扩展如下:

extension Binding where Value == KotlinMutableDictionary<NSString, NSMutableArray> {
    public func getPath(level1URI: String) -> Binding<[ScreenIdentifier]> {
        return Binding<[ScreenIdentifier]>(
            get: {
                let swiftDict = self.wrappedValue as! [NSString: NSMutableArray]
                guard let list = swiftDict[level1URI as NSString] else {
                    return []
                }
                return list as NSArray as! [ScreenIdentifier]
            },
            set: { newList in
                var swiftDict = self.wrappedValue as! [NSString: NSMutableArray]
                swiftDict[level1URI as NSString] = NSMutableArray(array: newList)
                self.wrappedValue = KotlinMutableDictionary(dictionary: swiftDict)
            }
        )
    }
}

关键修改点

  1. 扩展约束从Value == NSMutableDictionary改为Value == KotlinMutableDictionary<NSString, NSMutableArray>,匹配Kotlin MutableMap在Swift中的实际类型
  2. 移除不必要的NSMutableDictionary转换,直接基于KotlinMutableDictionary操作
  3. 添加空值判断,避免强制解包导致崩溃
  4. 赋值时直接使用KotlinMutableDictionary初始化方法,而非转成NSMutableDictionary

内容的提问来源于stack exchange,提问作者Daniele B

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 23:10:56