Passing casted Modified Image controls as parameters causes exception

I have an Image that I use foregroundColor on to modify it. Of course once it's modified it no longer is a plain Image. So then my struct init, that expects an Image parameter, requires that the modified Image get cast into an Image again. And this is what the XCode compiler recommends as well. However when I do that and run it it exceptions with: Could not cast value of type 'SwiftUI.ModifiedContent<SwiftUI.Image, SwiftUI._EnvironmentKeyWritingModifier<Swift.Optional<SwiftUI.Color>>>' (0x132846fa0) to 'SwiftUI.Image'

Here's some code.

struct PostButton: View {
  @EnvironmentObject var env: EnvironmentVM
   
  var body: some View {
    let primaryImage = Image(systemName: "plus.message").foregroundColor(getIconColor(env.darkMode.isOn)) as! Image
    let cameraImage = Image(systemName: "plus.message").foregroundColor(getIconColor(env.darkMode.isOn)) as! Image
    let messageImage = Image(systemName: "plus.message").foregroundColor(getIconColor(env.darkMode.isOn)) as! Image
     
    ExpandingButtonPanel(primaryButton: ExpandingButton(label: primaryImage), secondaryButtons: [
      ExpandingButton(label: cameraImage),
      ExpandingButton(label: messageImage)
    ])
  }
}

struct ExpandingButton: Identifiable {
  let id = UUID()
  let label: Image
  let action: (() -> Void)? = nil
}

struct ExpandingButtonPanel: View {
  @EnvironmentObject var env: EnvironmentVM
  @StateObject var vm: PostVM = PostVM()
  let primaryButton: ExpandingButton
  let secondaryButtons: [ExpandingButton]

Your code fragment is missing crucial information:

  • ExpandingButtonPanel is incomplete
  • You don't show your definition of getIconColor.
  • What is EnvironmentVM

If you can add these definitions, it may be possible to offer help.

Passing casted Modified Image controls as parameters causes exception
 
 
Q