The request of the index was outside the bounds of the array

Good day,

I get an error “The requested index was outside the bounds of the array.” How do I solve it? Here is my code:

import SwiftUI


struct ContentView: View {

    @State var first: [String] = ["Hello Jamie!","Hello John!","Hello Justin!"]

    @State var dates: [String] = ["21","22","23"]

    @State var numberIam: Int = 0

    var body: some View {

        VStack {

          ForEach(first, id: \.self) { item in

                Text(item)

                    .onAppear{

                        numberIam = numberIam + 1

                    }

                Text(dates[numberIam])

                 }

              }

    }

}

Thank You!

Your technique is very dangerous, because sooner or later, one of your arrays will be a different size, then the app will crash.

Why not gather the different data items into a struct...

struct MyData {
    let date: String
    let interval: String
    let duration: String
    let time: String
}

...and then use an array of that struct.

Also, when you name things, the name should reflect the type of that thing, so:

let date: Date
let interval: TimeInterval

...then, you can take advantage of all of Xcode's built-in functionality for comparing and operating on those types.

You're welcome.

The request of the index was outside the bounds of the array
 
 
Q