Home > OS >  Swift: Multiple Variables in ForEach Statement
Swift: Multiple Variables in ForEach Statement

Time:01-09

Here is my code, and I'm wondering how can I allow all 4 arrays to be used in the ForEach statement as right now I can only use 2.

struct ContentView: View {
var array1 = ["1", "2", "3"]
var array2 = ["a", "b", "c"]
var array3 = ["!", "@", "#"]
var array4 = [" ", "-", "~"]
      var body: some View {
                  VStack {
            HStack {
            Text("Upcoming Flights")
                    .font(.title2)
                .fontWeight(.bold)
            Spacer()
            }
            ScrollView(.horizontal) {
                HStack {
                    ForEach(Array(zip(array1, array2)), id: \.0) { item in
                    VStack {
                        Group {
                            Text("Flight")
                            Text(item.0)
                                .padding(.bottom)
                                .font(.caption)
                                .foregroundColor(.gray)
                            Text("Instructor")
                            Text(item.1)
                                .font(.caption)
                                .foregroundColor(.gray)
                        }
                    }
                    .frame(width: 110, height: 140)
                    .overlay {
                        RoundedRectangle(cornerRadius: 10)
                            .stroke(.gray.opacity(0.3), lineWidth: 1)
                    }
                    Spacer()
                    }
                }
                .frame(height: 200)
            }
            .frame(height: 200)
            .offset(y: -25)
        }
            .offset(y: -10)
            .padding([.leading, .trailing, .bottom])
     }
 }

Thanks in advance. @jnpdx I hope this helps, and I made sure it was reproducible. I don't want the arrays combined, but set as Text, like the first two arrays all in the same Stack in the view.

CodePudding user response:

With the way your data is currently structured, you could zip the arrays together into a multi-layered tuple and then extract the values. It's a little ugly.

struct ContentView: View {
    var array1 = ["1", "2", "3"]
    var array2 = ["a", "b", "c"]
    var array3 = ["!", "@", "#"]
    var array4 = [" ", "-", "~"]
    
    var arraysForLoop : [(String,String,String,String)] {
        let result = zip(array1,zip(array2,zip(array3,array4)))
        return result.map { ($0.0, $0.1.0, $0.1.1.0, $0.1.1.1) }
    }
    
    var body: some View {
        VStack {
            HStack {
                Text("Upcoming Flights")
                    .font(.title2)
                    .fontWeight(.bold)
                Spacer()
            }
            ScrollView(.horizontal) {
                HStack {
                    ForEach(arraysForLoop, id: \.0) { item in
                        VStack {
                            Group {
                                Text("Flight")
                                Text(item.0)
                                    .padding(.bottom)
                                    .font(.caption)
                                    .foregroundColor(.gray)
                                Text("Instructor")
                                Text(item.1)
                                    .font(.caption)
                                    .foregroundColor(.gray)
                                Text(item.2)
                                Text(item.3)
                            }
                        }
                        .frame(width: 110, height: 140)
                        .overlay {
                            RoundedRectangle(cornerRadius: 10)
                                .stroke(.gray.opacity(0.3), lineWidth: 1)
                        }
                        Spacer()
                    }
                }
                .frame(height: 200)
            }
            .frame(height: 200)
            .offset(y: -25)
        }
        .offset(y: -10)
        .padding([.leading, .trailing, .bottom])
    }
}

My suspicion is that long-term, you will decide that your current method of storing the data (eg multiple arrays of non-ID'd data) won't be an ideal structure. For instance, what if one array changes and the others don't? If the indexes change, all your data will be offset.

I'd recommend storing your data in one array where each item has each field you need (Instructor, Airplane, etc) -- this will make things much easier for you.

  •  Tags:  
  • Related