【问题标题】:Swift: Multiple Variables in ForEach StatementSwift:ForEach 语句中的多个变量
【发布时间】:2022-01-07 16:23:05
【问题描述】:

这是我的代码,我想知道如何在 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 我希望这会有所帮助,并且我确保它是可重现的。我不希望将数组合并,而是设置为 Text,就像视图中的前两个数组都在同一个 Stack 中一样。

【问题讨论】:

  • 很遗憾,您没有提供足够的信息来调试您遇到的问题。例如,ScheduledAirplaneScheduledInstructor 等是什么类型?它们都是 相同的 类型吗?你遇到什么错误?您可以添加一个minimal reproducible example 来帮助调试吗?
  • 对不起,第一个问题,感谢您的评论。所有这些(ScheduledAirplane、ScheduledInstructor、ScheduledTime、ScheduledDate)都是从 Firebase 获取的数据数组。目标是在重复的 VStack 中显示所有这些信息,其中每个阵列的信息不同。现在,我只有 ScheduledFlights 和 ScheduledInstructors,它们工作得非常好。我得到的错误是“在位置#3、#4 调用中的额外参数”,这很明显,因为我意识到一个 zip 中只允许两个变量,来自 Q 中的链接。我希望这就是你所需要的。
  • 有很多可能性here。有没有回答你的问题?
  • 我想这可能是一个解决方案,但是怎么可能把它一起列出来。目标是有这样的东西: 教练:_________ 飞机:_________ 日期:________ 时间:________ 全部在单独的 Vstacks 中的滚动视图 ForEach 语句中。有点混乱,抱歉。
  • 你没有提供任何关于你的模型的信息,所以我不知道你的观点的细节。在我看来,您问题的症结在于如何组合多个数组——我链接到的问题列出了许多不同的方法(zip 除外)。您尝试过其中任何一个吗?

标签: arrays swift foreach


【解决方案1】:

根据您的数据当前的结构方式,您可以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 { ($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])
    }
}

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

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

【讨论】:

    猜你喜欢
    • 2010-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-01
    • 1970-01-01
    • 2016-06-05
    • 1970-01-01
    相关资源
    最近更新 更多