Swift: ForEach 语句中的多个变量

Swift: Multiple Variables in ForEach Statement

这是我的代码,我想知道如何允许在 ForEach 语句中使用所有 4 个数组,因为现在我只能使用 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])
     }
 }

提前致谢。 @jnpdx 我希望这会有所帮助,并且我确保它是可重现的。我不想将数组合并,而是设置为文本,就像视图中的前两个数组都在同一个堆栈中一样。

根据您的数据当前的结构方式,您可以 zip 将数组组合成一个多层元组,然后提取值。有点丑。

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 { ([=10=].0, [=10=].1.0, [=10=].1.1.0, [=10=].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])
    }
}

我怀疑从长远来看,您会认为当前存储数据的方法(例如,非 ID 数据的多个数组)不是理想的结构。例如,如果一个数组发生变化而其他数组没有变化怎么办?如果索引发生变化,您的所有数据都将被偏移。

我建议将您的数据存储在 one 数组中,其中每个项目都有您需要的每个字段(教练、飞机等)——这将使事情 很多对你来说更容易。