ForEach and Arrays | 4 Arrays in 1 ForEach loop

Good day,

How do I repeat the arrays but in one ForEach loop or is there any other way of repeating the arrays in the order dates then intervals then durations then times, I need repeated blocks of multiple arrays.

Here is my code:


struct ContentView: View {

    @State var dates: [String] = ["21 Jun","22 Jul","12 Aug"]
    @State var intervals: [String] = ["8","2","5"]
    @State var durations: [String] = ["10m","12m","7m"]
    @State var times: [String] = ["45s","65s","12s"]

    var body: some View {

        VStack {

            ForEach(Array(zip(dates, durations)), id: \.0) { item in 

                VStack{

                Text(item.0)

                    .padding()

                Text(item.1)

                        .padding()

                }
                

          }

            ForEach(Array(zip(intervals, times)), id: \.0) { item in 

                VStack{

                    Text(item.0)

                        .padding()

                    Text(item.1)

                        .padding()

                }

            }

            .offset(y: -206)

        }

    }

}

            

Here is the order I need it to be repeated in:

Thank You

Answered by DTS Engineer in 730026022

Hello,

If I'm understanding what you'd like to achieve correctly, I think you would benefit from creating a struct that represents a single "item" in your UI, for example:

struct Item: Identifiable {
    
    // Make sure this ID is unique.
    var id: String {
        date
    }
    
    var date: String
    var interval: String
    var duration: String
    var time: String
}

Then, you could either create an @State variable that holds an Array of these Items, and iterate over that in a ForEach, or you could compute an Array of Items from your other state variables, for example:

struct ContentView: View {

    @State var dates: [String] = ["21 Jun","22 Jul","12 Aug"]
    @State var intervals: [String] = ["8","2","5"]
    @State var durations: [String] = ["10m","12m","7m"]
    @State var times: [String] = ["45s","65s","12s"]
    
    var items: [Item] {
        
        let count = dates.count
        
        // Make sure all of the arrays have the same number of elements.
        precondition(count == intervals.count)
        precondition(count == durations.count)
        precondition(count == times.count)
        
        var items: [Item] = []
        for index in 0..<count {
            let item = Item(date: dates[index],
                            interval: intervals[index],
                            duration: durations[index],
                            time: times[index])
            items.append(item)
        }
        
        return items
    }

    var body: some View {
        
        ScrollView {
            ForEach(items) { item in
                VStack{
                    Text(item.date)
                    Text(item.interval)
                    Text(item.duration)
                    Text(item.time)
                }
                .padding()
            }
        }
        
    }
}
Accepted Answer

Hello,

If I'm understanding what you'd like to achieve correctly, I think you would benefit from creating a struct that represents a single "item" in your UI, for example:

struct Item: Identifiable {
    
    // Make sure this ID is unique.
    var id: String {
        date
    }
    
    var date: String
    var interval: String
    var duration: String
    var time: String
}

Then, you could either create an @State variable that holds an Array of these Items, and iterate over that in a ForEach, or you could compute an Array of Items from your other state variables, for example:

struct ContentView: View {

    @State var dates: [String] = ["21 Jun","22 Jul","12 Aug"]
    @State var intervals: [String] = ["8","2","5"]
    @State var durations: [String] = ["10m","12m","7m"]
    @State var times: [String] = ["45s","65s","12s"]
    
    var items: [Item] {
        
        let count = dates.count
        
        // Make sure all of the arrays have the same number of elements.
        precondition(count == intervals.count)
        precondition(count == durations.count)
        precondition(count == times.count)
        
        var items: [Item] = []
        for index in 0..<count {
            let item = Item(date: dates[index],
                            interval: intervals[index],
                            duration: durations[index],
                            time: times[index])
            items.append(item)
        }
        
        return items
    }

    var body: some View {
        
        ScrollView {
            ForEach(items) { item in
                VStack{
                    Text(item.date)
                    Text(item.interval)
                    Text(item.duration)
                    Text(item.time)
                }
                .padding()
            }
        }
        
    }
}
ForEach and Arrays | 4 Arrays in 1 ForEach loop
 
 
Q