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

如何修复'Binding<Device>'无法转换为期望参数类型'Device'的错误

问题原因

你外层ForEach传入的是绑定类型的数组$deviceArrays.devices,因此遍历得到的deviceArray为Binding<DeviceArray>类型,内层遍历deviceArray.row拿到的device实际是Binding<Device>类型,和setCurrentDevice函数要求的普通Device参数类型不匹配。

解决方案

提供两种可选方案,按需选择:

  • 方案1:直接取绑定值的包装值,无需改动其他逻辑
    仅修改onAppear闭包内的传参,调用Binding类型的wrappedValue属性获取底层的普通Device实例即可:
    .onAppear {
        setCurrentDevice(to: device.wrappedValue)
    }
    
  • 方案2:调整遍历逻辑,直接遍历普通数组
    如果你当前场景下不需要对deviceArray或device做修改操作,可直接去掉外层ForEach参数前的$符号,遍历普通数组即可直接得到Device类型的实例:
    ForEach(deviceArrays.devices, id: \.id) { deviceArray in
         HStack {
              ForEach(deviceArray.row, id: \.id) { device in
                   AnotherView(device: $currentDevice, size: $size)
                       .onAppear {
                            setCurrentDevice(to: device)
                        }
            }
        }
    }
    

内容的提问来源于stack exchange,提问作者Mark Reggiardo

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.05 00:42:00