title: Getting index out of bounds exception when accessing array SwiftUI
body: <p>UPDATE:
  after adding an observed object I am still getting an index out of bounds error. This is all the code that I am using starting at the first view to the last</p>
  <p>Parent:</p>
  <pre><code>struct DetailedView: View {
      
      @Environment(\.managedObjectContext) var moc
      
      var targetMuscle : String = &quot;Chest&quot;
      let today = Date()
      @State public var exerciseCards : [ExerciseCard] = []
      @State public var exercise : String = &quot;Bench Press&quot;
      @State public var exercises : Int = 0
      @State private var showPassedWorkouts : Bool = false
      @State private var savedWorkout : Bool = false
      @State private var exerciseNames : [String] = []
      @State private var exerciseRows : [[ExerciseTableRow]] = [[]]
      
      //@State private var exerciseSets : [[Int16]] = [[]]
      //@State private var setWeight : [[Int16]] = [[]]
      //@State private var setReps : [[Int16]] = [[]]
      
      @ObservedObject var viewModel = ViewModel()
      @State var exerciseIndex : Int = 0
      
      static let taskDateFormat : DateFormatter = {
          let formatter = DateFormatter()
          formatter.dateStyle = .long
          return formatter
      }()
      
      var body: some View {
          ZStack{
              VStack{
                  HStack{
                      VStack{
                          Text(targetMuscle).font(.system(size:40)).fontWeight(.medium)
                          Text(&quot;\(today, formatter: Self.taskDateFormat)&quot;)
                              .font(.system(size:20))
                      }.frame(width: 250, height: 30, alignment: .topLeading)
                          .navigationBarTitle(&quot;&quot;)
                          .navigationBarHidden(true)
                          .padding(.bottom, -7)
                      Button(action: {
                          self.showPassedWorkouts.toggle()
                      }) {
                          Text(&quot;Passed Workouts&quot;)
                              .multilineTextAlignment(.center)
                          
                          
                      }.offset(x: -75, y: 25)
                          .sheet(isPresented: $showPassedWorkouts){
                              PassedWorkoutList()
                      }
                      
                      
                      Button(action: {
                          
                          for number in 0..&lt;self.exerciseCards.count{
                              print(self.exerciseCards[number].exercise)
                              /*for innernum in 0..&lt;self.exerciseCards[number].tableRows.count{
                                  print(self.exerciseCards[number].tableRows[innernum].setReps)
                                  print(self.exerciseCards[number].tableRows[innernum].setWeight)
                                  print(self.exerciseCards[number].tableRows[innernum].set)
                              }*/
                          }
                          
                          /*let workout = Workout(context: self.moc)
                          workout.muscle = self.targetMuscle
                          workout.date = formattedDateString(day: self.today)
                          
                          for number in 0..&lt;self.exerciseCards.count{
                              let exercise = Exercise(context: self.moc)
                              exercise.name = self.exerciseCards[number].exercise
                              for innerNum in 0..&lt;self.exerciseCards[number].tableRows.count{
                                  let exerciseSet = ExerciseSet(context: self.moc)
                                  exerciseSet.reps = Int16(self.exerciseCards[number].tableRows[innerNum].reps) ?? 0
                                  exerciseSet.weight = Int16(self.exerciseCards[number].tableRows[innerNum].weight) ?? 0
                                  exerciseSet.set = self.exerciseCards[number].tableRows[innerNum].set
                                  exercise.addToExerciseSet(exerciseSet)
                              }
                              workout.addToExercise(exercise)
                          }
                          
                          try? self.moc.save()*/
                          
                          
                      }) {
                          Text(&quot;Finish&quot;)
                      }.offset(x: -20, y: 20)
                  }.padding(.bottom, 35)
                      .padding(.leading)
                  
                  
                  
                  ScrollView{
                      ForEach(0..&lt;exerciseCards.count, id: \.self){ number in
                          self.exerciseCards[number]
                      }
                      
                      
                      Button(action: {
                          
                          //let tempExerciseRow : [ExerciseTableRow] = []
                          //let tempExerciseSets : [Int16] = []
                          let tempSetReps : [Int16] = []
                          let tempSetWeight : [Int16] = []
                          
                          self.viewModel.setReps.append([Int16(12), Int16(12)])
                          self.viewModel.setWeight.append([Int16(12), Int16(12)])
                          
                          
                          print(self.viewModel.setReps)
                          
                          
                          
                          print(self.viewModel.setReps)
                          print(self.viewModel.setWeight)
                          
                          self.exerciseNames.append(&quot;&quot;)
                          //self.exerciseRows.append(tempExerciseRow)
                          //self.exerciseSets.append(tempExerciseSets)
                          /*self.setWeight.append(tempSetWeight)
                          self.setReps.append(tempSetReps)
                          
                          self.exerciseCards.append(ExerciseCard(exercise: self.$exerciseNames[self.exerciseNames.count - 1], tableRows: self.$exerciseRows[self.exerciseNames.count - 1], tableSet: self.$exerciseSets[self.exerciseNames.count - 1], setReps: self.$setReps[self.exerciseNames.count - 1], setWeight: self.$setWeight[self.exerciseNames.count - 1]))*/
                          
                          
                          self.exerciseCards.append(ExerciseCard(exercise: self.$exerciseNames[self.exerciseNames.count - 1],viewModel: self.viewModel,index : self.exerciseIndex))
                          
                          self.exerciseIndex += 1
                          
                          //self.exerciseCards.append(ExerciseCard(exercise: self.$exerciseNames[self.exerciseNames.count - 1], tableRows:self.$exerciseRows[self.exerciseNames.count - 1]))
                          
                          
                      }) {
                          Text(&quot;Add Exercise&quot;)
                              .frame(minWidth: 325)
                              .padding()
                              .foregroundColor(.white)
                              .background(Color.blue.opacity(0.7))
                              .cornerRadius(20)
                          
                      }.padding(.top)
                          .frame(width: 400)
                  }
                  
                  
              }
          }.background(Color.white)
              
      }
      
  }
  </code></pre>
  <p>First Child view</p>
  <pre><code>struct ExerciseCard: View {
      @Binding public var exercise : String
      @State public var tableRows : [ExerciseTableRow] = []
      
      @ObservedObject var viewModel : ViewModel
      var index : Int = 0
      
      //@Binding public var tableSet : [Int16]
      //@Binding public var setReps : [Int16]
      //@Binding public var setWeight : [Int16]
      
      @State var numberOfRow : Int = 0
      
      
      var body: some View {
          VStack{
              TextField(&quot;Enter Exercise&quot;, text: $exercise).textFieldStyle(RoundedBorderTextFieldStyle())
              .frame(width: 300)
              .multilineTextAlignment(.center)
              HStack{
                  Group{
                      Text(&quot;Set&quot;)
                      Text(&quot;Weight&quot;)
                      Text(&quot;Reps&quot;)
                  }.padding(.horizontal, 30)
                  .offset(x: -20, y: 0)
                  
              }
              VStack{
                  
                  ForEach(0..&lt;tableRows.count, id: \.self){ number in
                      self.tableRows[number]
                      
                      
                  }
              }.padding(.bottom, 5)
              
              HStack{
                  Button(action: {
                      if self.tableRows.count &gt; 1{
                          self.tableRows.remove(at: self.tableRows.count-1)
                      }
                          
                  }) {
                      Text(&quot;Remove Set&quot;)
                          .frame(minWidth: 150)
                          .padding(.vertical, 5)
                          .foregroundColor(.white)
                          .background(Color.red)
                          .cornerRadius(20)
                      
                  }
                  
                  Button(action: {
                      
                      /*print(self.tableSet.isEmpty)
                      print(self.setReps.isEmpty)
                      print(self.setWeight.isEmpty)
                      
                      
                      
                      self.tableSet.append(Int16(self.numberOfRows + 1))
                      self.setReps.append(Int16(0))
                      self.setWeight.append(Int16(0))
                      
                      print(self.tableSet[self.numberOfRows])
                      print(self.setReps[self.numberOfRows])
                      print(self.setWeight[self.numberOfRows])
                      
                      self.tableRows.append(ExerciseTableRow(set: self.$tableSet[self.tableSet.count - 1], setWeight: self.$setWeight[self.setWeight.count - 1], setReps: self.$setReps[self.setReps.count - 1]))*/
                      
                      print(self.index)
                      print(self.numberOfRow)
                      
                      
                      //self.viewModel.setReps[self.index][self.numberOfRow] = Int16(0)
                      //self.$viewModel.setWeight[self.index][self.numberOfRow]
                      
                      self.tableRows.append(ExerciseTableRow(exerciseIndex: self.index, rowIndex: self.numberOfRow, viewModel: self.viewModel, set: self.numberOfRow + 1))
                      
                      self.numberOfRow += 1
                      
                  }) {
                      Text(&quot;Add Set&quot;)
                          .frame(minWidth: 150)
                          .padding(.vertical, 5)
                          .foregroundColor(.white)
                          .background(Color.green)
                          .cornerRadius(20)
                      
                  }
                  
              }
          }
          .padding()
          .padding(.vertical)
          .background(Color.offWhite)
          .cornerRadius(20)
          .shadow(color: Color.black.opacity(0.2), radius: 10, x:10, y:10)
          .shadow(color: Color.white.opacity(0.7), radius: 10, x:-5, y:-5)
      }
  }
  </code></pre>
  <p>Second Child view (This is where I am getting the array out of bounds error. when I try to use my 2d array with a text field)</p>
  <pre><code>struct ExerciseTableRow: View {
      //@State public var weight : String = &quot;0&quot;
      //@State public var reps : String = &quot;0&quot;
      //@Binding var set : Int16
      //@Binding var setWeight : Int16
      //@Binding var setReps : Int16
      
      var exerciseIndex : Int
      var rowIndex : Int
      @ObservedObject var viewModel : ViewModel
      var set : Int
      
      var body: some View {
          
          HStack{
              Text(String(set))
                  .padding(.trailing, 40)
                  .padding(.leading, 10)
              
              Group{
                  TextField(&quot;0&quot;, value: self.$viewModel.setWeight[exerciseIndex][rowIndex], formatter: NumberFormatter())
                  TextField(&quot;0&quot;, value: self.$viewModel.setReps[exerciseIndex][rowIndex], formatter: NumberFormatter())
              }.textFieldStyle(RoundedBorderTextFieldStyle())
                  .frame(width: 50)
                  .multilineTextAlignment(.center)
                  .keyboardType(.numberPad)
                  .padding(.horizontal, 30)
              
              
          }
          
          
      }
  }
  </code></pre>