Search code examples
swiftswiftuicore-graphics

Getting a `Type of expression is ambiguous without more context` when trying to returning a `some View` with a `ZStack` object


I'm trying to use CoreGraphics to draw some musical notes from a struct that is defined elsewhere in a swiftui App

func drawSheetMusic(in size: CGSize) -> some View {
    return ZStack {
        Color.clear.drawingGroup { ctx in
            for note in self.musicData.notes {
                let rect = CGRect(x: note.position.x - note.radius, y: note.position.y - note.radius, width: 2 * note.radius, height: 2 * note.radius)
                ctx.cgContext.addEllipse(in: rect)
                ctx.cgContext.setFillColor(Color.black.cgColor)
                ctx.cgContext.fillPath()
            }
            return Rectangle().fill(Color.clear)
        }
    }
}

But this is returning an error Type of expression is ambiguous without more context on Line 2 there. What exactly am I doing wrong here?


Solution

  • func drawingGroup(opaque: Bool = false, colorMode: ColorRenderingMode = .nonLinear) -> some View
    

    doesn't take a closure parameter. The problem becomes obvious if you remove the ZStack.

    You seem to be confusing this with

    UIGraphicsImageRenderer.image(actions: (UIGraphicsImageRendererContext) -> Void) -> UIImage