1

I didn't change anything in my code, but since iOS16 the onAppear() part is not working. selectedParcoursIndexArray.append(0) does not execute, And app crash on :

 get: { self.selectedParcoursIndexArray[num] },

Because selectedParcoursIndexArray is empty. And should be filled while VStack appear.

My code :

 import SwiftUI

struct DepartsSimultanesView: View {
    @EnvironmentObject var objCourse : CourseActuelle
    @EnvironmentObject var zindex : Zindex
    @EnvironmentObject var listActuelle : ListActuelle
    @State var selectedParcoursIndexArray : [Int] = [0]
    @State var peutSauvegarder = false
    @State var groupeManager : GroupeManager
    var listeParcoursRestants : [String] = []
    @State private var action: Int? = 0
    
    var body: some View {
        
        VStack{
            HStack{
                ...
            }
        HStack{
          ...
            
        }
        Form{
            Section(header: Text("Coureurs/Choix parcours")){
                ForEach(0 ..< groupeManager.groupeList.count, id:\.self){ num in
                    
                    let detailManager = DetailManager(groupeId: groupeManager.groupeList[num].id, courseId: objCourse.id!)
                    let listeParcoursRestants = listRestants(detailManager: detailManager)
                    let enCourse = detailManager.enCourseList.filter { $0.groupeId == groupeManager.groupeList[num].id }
                    Picker(selection: Binding(
                            get: { self.selectedParcoursIndexArray[num] },
                            set: { (newValue) in
                                self.selectedParcoursIndexArray[num] = newValue
                                peutSauvegarder = true
                            }), label: HStack{
                        Image(systemName: "person.fill")
                        Text("\(groupeManager.groupeList[num].nomGroupe)")
                            }.foregroundColor(selectedParcoursIndexArray[num] != 0 ? .orange : Color("ColorDarkLight"))
                    ) {
                        
                        if enCourse.count != 0 {
                          ...
                        }
                    }.navigationBarHidden(true)
                    .disabled(listeParcoursRestants.count == 1 || enCourse.count != 0)

                }
            }
        }
        }
        .onAppear(){
            groupeManager = GroupeManager(courseId: objCourse.id!)
            for _ in (0 ..< groupeManager.groupeList.count) {
                selectedParcoursIndexArray.append(0)

            }
        }
            
        .navigationBarHidden(true)
        NavigationLink(destination: CestPartiView().environmentObject(objCourse), tag: 1, selection: $action){}
    }

}
Clément Tengip
  • 618
  • 6
  • 19

1 Answers1

0

ForEach is not a for loop, it'll crash if you try to access an array by index inside its closure. id:\.self is a mistake, the id needs to be a property of the data it cannot be the data itself.

onAppear is designed to call an external action not to initialise the data that will be displayed. Move that initialisation somewhere else like to an @State struct's init.

malhal
  • 26,330
  • 7
  • 115
  • 133