无法在 SwiftUI 中将类型“图像”的值转换为预期的参数类型“字符串”

Posted

技术标签:

【中文标题】无法在 SwiftUI 中将类型“图像”的值转换为预期的参数类型“字符串”【英文标题】:Cannot convert value of type 'Image' to expected argument type 'String' in SwiftUI 【发布时间】:2021-03-19 08:51:11 【问题描述】:

我有菜单按钮,它有系统图像信封.fill

 MenuButton(name: "Catalogue", image:"envelope.fill", selectedMenu: 
 $menuData.selectedMenu,animation: 
  animation)

但是当更改自定义图像时,它会抛出错误“无法将'Image'类型的值转换为预期的参数类型”和想法?

  MenuButton(name: "Catalogue", Image("custom-image"), selectedMenu: 
 $menuData.selectedMenu,animation: animation)
            

【问题讨论】:

【参考方案1】:

您似乎在第二次调用中缺少参数image。它应该看起来像 MenuButton 参数的设置方式。

MenuButton(name: "Catalogue",
           image: Image("custom-image"),
           selectedMenu: $menuData.selectedMenu,
           animation: animation)

所以你的菜单按钮声明应该是这样的:

MenuButton(name: String,
           image: Image,
           selectedMenu: MenuData,
           animation: Animation)

【讨论】:

tnx 这么多,但仍然说“无法将 'Image' 类型的值转换为预期的参数类型 'String'” 这是因为您必须交换 image 参数以接受 Image 而不是 String。看看我分享给你的代码的第二部分。如果您仍然卡住,您可以使用“MenuButton”声明更新您的问题吗?

以上是关于无法在 SwiftUI 中将类型“图像”的值转换为预期的参数类型“字符串”的主要内容,如果未能解决你的问题,请参考以下文章