Home > Mobile >  Avoid forcing to return Image (SwiftUI)
Avoid forcing to return Image (SwiftUI)

Time:07-27

  struct ContentView: View {
  var body: some View {
    imageWithCircle(sfsymbol: "shared.with.you", width: 30, height: 30)
           
  }
func imageWithCircle(sfsymbol: String, width: CGFloat, height: CGFloat) -> Image {
    return Image(systemName: sfsymbol)
        .frame(width: width, height: height)
        .background(
            Circle()
                .fill(.blue)
                .frame(width: width   5, height: height   5)
        )
}

  }

I'm receiving this error "Cannot convert return expression of type 'some View' to return type 'Image'" "Insert as! Image".

I have only one return view of Image and return as Image, why would the compiler ask me to force return type?

CodePudding user response:

If you return only Image, the error would not occur.

However, you are trying to return an Image with Modifiers, so this should be a some View type instead of Image.

Try this:

func imageWithCircle(sfsymbol: String, width: CGFloat, height: CGFloat) -> some View {}
  • Related