如何存储变量的值并在下一次 for 循环迭代中使用它们

How to store value of variables and use them in the next for loop iteration

提问人:Peter Rice 提问时间:9/2/2023 最后编辑:Ken WhitePeter Rice 更新时间:9/2/2023 访问量:106

问:

这是包含 150 行的表的第 2 到第 7 列,我将其分隔到一个新文件中,以便于阅读和维护。 在 for 循环中,变量应更新并存储,以便在逻辑运行后的下一次循环迭代中使用;但现在变量在一次迭代后被重置。 如何让变量可以存储到下一次循环迭代中?

import SwiftUI
class StockSymbol1Data: ObservableObject {
    
    let secondaryRally = 1
    let naturalRally = 2
    let upwardTrend = 3
    let downwardTrend = 4
    let naturalReaction = 5
    let secondaryReaction = 6
    
    @Published var currentColumn: Double?
    @Published  var lastUpwardTrend: Double?
    @Published  var lastSecondaryRally: Double?
    @Published  var lastSecondaryReaction: Double?
    @Published  var lastNaturalRally: Double?
    @Published  var lastNaturalReaction: Double?
    @Published  var lastDownwardTrend: Double?
    @Published  var blackLineDownwardTrend: Double?
    @Published  var blackLineNaturalRally: Double?
    @Published  var redLineUpwardTrend: Double?
    @Published  var redLineNaturalReaction: Double?
}
struct StockSymbol1ColumnView: View {
    let close1: Double
    let headers: [String]
    let scrollViewWidth: CGFloat

    let priceMovePts: Double = 6
    let furtherPriceMovePts: Double = 3
    @ObservedObject var stockSymbol1Data: StockSymbol1Data
    var body: some View {
        ForEach(0..<6, id: \.self) { columnIndex in
            let columnClose: Double? = {
                switch columnIndex {
                case 0:
                    // Apply your rule here for Secondary Rally column
            return stockSymbol1Data.lastSecondaryReaction == close1 ? close1 : nil
        case 1:
                    // Apply your rule here for Natural Rally column
            return stockSymbol1Data.lastNaturalRally == close1 ? close1 : nil
        case 2:
                    // Apply your rule here for Upward Trend column
            return stockSymbol1Data.lastUpwardTrend == close1 ? close1 : nil
        case 3:
                    // Apply your rule here for DownwardTrend column
            return stockSymbol1Data.lastDownwardTrend == close1 ? close1 : nil
        case 4:
                    // Apply your rule here for NaturalReaction column
            return stockSymbol1Data.lastNaturalReaction == close1 ? close1 : nil
        case 5:
                    // Apply your rule here for Secondary Reaction column
             return stockSymbol1Data.lastSecondaryReaction == close1 ? close1 : nil
                    return nil
                }
            }()

            let textColor: Color = {
                switch columnIndex {
                case 2:
                    // Change text color for Upward Trend column to black
                    return .black
                case 3:
                    // Change text color forDownward Trend column to red
                    return .red
                default:
                    return .gray
                }
            }()

            Text(columnClose != nil ? "\(columnClose ?? 0.0)" : "")
                .frame(width: scrollViewWidth / CGFloat(headers.count), height: 15)
                .border(Color.gray, width: 2)
                .padding(.horizontal, 5)
                .foregroundColor(textColor)
        }
    }
}

我试过添加 Self。方法或其他方法,但新的循环迭代只是重置变量,无法获取上一个循环的结果和更新。我希望变量在一次循环迭代中更新,并且可以在随后的循环迭代中引用。

Swift for 循环 变量 SwiftUI

评论

2赞 lorem ipsum 9/2/2023
ForEach 并不是真正的循环,它只是遍历呈现视图的集合,在传统循环中,迭代在它们呈现的 ForEach 中一个接一个地发生,任何计算都应该准备好在 ForEach 呈现时显示。视图用于 UI,而不是“工作”。
0赞 Peter Rice 9/2/2023
感谢您的评论..但问题是变量不能传递到下一行。如何解决?
4赞 lorem ipsum 9/2/2023
创建一个“ConfigModel”或“ListModel”,它执行所有数学运算并保存您需要的一切,以便可以显示。structForEach
0赞 Peter Rice 9/3/2023
谢谢。。因为在表的第一列上是日期,而 close1 的值是基于日期的,所以逻辑块应该在 ForEach...将逻辑分离到另一个文件并在 ForEach 中调用后,问题保持不变。变量在每个新行上重置,但我想在 eqch 行上保留变量的更新,并使用它们与后续行上的新 close1 值进行比较。这就是逻辑的运行方式,逻辑主要将更新的变量与该日期的新收盘价1进行比较
0赞 lorem ipsum 9/3/2023
我们确切地知道您想做什么,这不是问题。问题在于,您期望 ForEach 能够执行您想要执行的任务,您必须创建逻辑,必须准备数据,以便 ForEach 能够显示数据。

答: 暂无答案