Type ‘()’ cannot conform to ‘AccessibilityRotorContent’

Driving me nuts… I have a class called TrackPiece2 with a bunch of var’s in it… I have an array of these and I want to change a value in each TrackPiece2 of the array…

but I am getting the error “Type ‘()’ cannot conform to ‘AccessibilityRotorContent’” on the ForEach line of func changeZoom below

` class TrackLayout2 {

    var trackPieces: [TrackPiece2] = []

    var drawOuterLines: Bool = false

    

    func addTrackPiece(newTrkPc: TrackPiece2) {

        self.trackPieces.append(newTrkPc)

    }

    

    func changeZoom(newZoom: CGFloat) {

        ForEach(trackPieces) {

            trkPc in

            trkPc.zoom = newZoom

        }

    }

    

    func loadTrackPieces() {

        let centerPoint = CGPoint(x: 160, y:160)

        

        addTrackPiece(newTrkPc: TrackPiece2(centerPnt: centerPoint, radius: 160.0, width: 10.0, startAngle: Angle(degrees: 0), angleArc: Angle(degrees: 45), color: Color.pink, zoom: 0.5))

    }

} `

Thanks for any advice/help… TJ

Interesting… but if I replace the entire ForEach {} with a specific member of the array, ‘trackPieces[0].zoom = newZoom’, it works fine…

Also, TrackPiece2 is Identifiable…

Let's first format the code to make it more readable:

class TrackLayout2 {
    var trackPieces: [TrackPiece2] = []
    var drawOuterLines: Bool = false
    
    func addTrackPiece(newTrkPc: TrackPiece2) {
        self.trackPieces.append(newTrkPc)
    }
    
    func changeZoom(newZoom: CGFloat) {
        ForEach(trackPieces) {
            trkPc in
            trkPc.zoom = newZoom
        }
    }
    
    func loadTrackPieces() {
        let centerPoint = CGPoint(x: 160, y:160)
        
        addTrackPiece(newTrkPc: TrackPiece2(centerPnt: centerPoint, radius: 160.0, width: 10.0, startAngle: Angle(degrees: 0), angleArc: Angle(degrees: 45), color: Color.pink, zoom: 0.5))
    }
}

The problem is with ForEach.

ForEach is a SwiftUI dynamic View container, to build views in cycle. For action you have to use regular swift for .. in expression

see details here https://stackoverflow.com/questions/63199788/foreach-loop-inside-button-action-block-throws-type-cannot-conform-to-view

So you could try to change in:

    func changeZoom(newZoom: CGFloat) {
        for trkPc in trackPieces {
            trkPc.zoom = newZoom
        }
    }

new problem, trkPc is a constant (probably TrackPiece2 is a struct, so here you copy by value and create trkPc as let).

So you could try to change again :

    func changeZoom(newZoom: CGFloat) {
        for var trkPc in trackPieces {
            trkPc.zoom = newZoom
        }
    }

This now compiles but will not change the value in the array, only the copy (trkPc) of each item will be changed.

If TrackPiece2 were defined as a class, then you would pass by reference and do update the original array.

If you want to do it with struct you have to access directly to array items, as you have found:

    func changeZoom(newZoom: CGFloat) {
        if trackPieces.count > 0 {
            for index in 0..<trackPieces.count {
                trackPieces[index].zoom = newZoom
             }
        }
    }
Type ‘()’ cannot conform to ‘AccessibilityRotorContent’
 
 
Q