'How do I make the elements in an array a state variable using swift

I have a view that puts into a horizontal stack images. The name of the image is in an array I create from an environmentObject. When I change a property of the enviromentObject I want the horizontal stack to reflect that change

    import SwiftUI
    
    struct Train: View {
        @EnvironmentObject var lr: RoutineModel
        @Binding var didChange : Bool
        
        init(didChange: Binding<Bool>) {
            _didChange = didChange
        }
        
       // var names : [String]  = []
    
        var body: some View {
          //MARK: figure out how to use lr OR  fetched routine to put up squares and blank if not picked
            let names =  [lr.centering, lr.breath, lr.meditation, lr.warmup, lr.sunsalutation, lr.peak, lr.balance, lr.backbend, lr.twist, lr.sittingFF, lr.luscious, lr.endpose, lr.shavasana]
            if didChange {
                ScrollView(.horizontal, showsIndicators: false) {
                    HStack(alignment: .top, spacing: 20) {
                        ForEach (names, id: \.self) { n in
                            Image(n)
                                .renderingMode(.original)
                                .resizable()
                                .frame(width: 50, height: 50)
                        }
    
                    }
                    
                    
                }
            }
            
        }//end of body
    }// end of struct

The user will choose the name of the image in a different view. When the user chooses lr.centering I want to put up the first image. Then next when they choose lr.breath I want to put up two images (lr.centering and lr.breath) etc.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source