I am currently trying to refactor some of my variables that I am using within a View into a struct for clarity. However, when I try to do this, I get a bunch of errors such as Value of type 'Binding<DrawableStaff>' has no dynamic member 'drawings' using key path from root type 'DrawableStaff'.
How do I fix these issues? I have looked into the documentation, and I have looked around online, and I haven't found a solution to this issue.
Here is what the refactored struct looks like
Here is where DrawableStaff appears in my main ContentView
Here is where DrawingPad is defined:
Here is my Drawing definition:
How do I fix these issues? I have looked into the documentation, and I have looked around online, and I haven't found a solution to this issue.
Here is what the refactored struct looks like
Code Block struct DrawableStaff{ var drawing: Drawing = Drawing() var drawingList: [Drawing] = [Drawing]() var paths: [Path] = [Path]() var PathHolder = Path() var color = Color.primary var lineWidth: CGFloat = 3.0 }
Here is where DrawableStaff appears in my main ContentView
Code Block struct ContentView: View { @State private var staff1DrawableStaff = DrawableStaff() var body: some View { VStack { let staff1 = ZStack{ DrawingPad(drawableStaff: $staff1DrawableStaff) } } }
Here is where DrawingPad is defined:
Code Block struct DrawingPad: View { @Binding var drawableStaff: DrawableStaff var body: some View { GeometryReader { geometry in Path { path in for drawing in self.drawableStaff.drawings { self.add(drawing: drawableStaff.drawing, toPath: &path) } self.add(drawing: self.drawableStaff.currentDrawing, toPath: &path) self.drawableStaff.pathHolder = path } .stroke(self.drawableStaff.color, lineWidth: self.drawableStaff.lineWidth) .background(Color(UIColor.systemBackground)) .gesture( DragGesture(minimumDistance: 0.1) .onChanged({ (value) in let currentPoint = value.location if currentPoint.y >= 0 && currentPoint.y < geometry.size.height { self.drawableStaff.currentDrawing.points.append(currentPoint) } }) .onEnded({ (value) in self.drawableStaff.drawings.append(self.currentDrawing) self.drawableStaff.currentDrawing = Drawing() }) ) } .frame(maxHeight: .infinity) } private func add(drawing: Drawing, toPath path: inout Path) { let points = drawing.points if points.count > 1 { for i in 0..<points.count-1 { let current = points[i] let next = points[i+1] path.move(to: current) path.addLine(to: next) } } } }
Here is my Drawing definition:
Code Block struct Drawing { var points: [CGPoint] = [CGPoint]() }