【问题标题】:cannot use for...in or forEach loop in swift to print views in swift不能在 swift 中使用 for...in 或 forEach 循环来快速打印视图
【发布时间】:2022-01-13 21:22:02
【问题描述】:

所以这就是问题所在。当我在文件中给出值时,我的代码运行良好。但是,我制作了一个单独的模型文件,我想从中迭代 WeatherDay 对象数组以打印它们以使它们动态化。但是,从逻辑上讲,这听起来很容易,但我遇到了这些烦人的错误,这些错误不会让我编译。任何人帮助!代码如下:

struct ContentView: View {
    
    @State private var isNight = false
    
    var body: some View {
        ZStack {
            var selectedWeather = getWeatherValues()
            // binding basically enforces that the value for the object stays the same
            BackgroundView(isNight: $isNight)
            
            VStack {
                let days = selectedWeather.getWeatherDayListData()
                CityTextView(cityName: selectedWeather.getCity())
                
//                first object
                mainWeatherView(isNight: $isNight, todayWeather:
                                    selectedWeather.getWeatherDayListData()[0])

                HStack(spacing: 20) {
                    weatherDayView(weatherDays: selectedWeather.getWeatherDayListData())

                }
                
                
            }
            Spacer() //basically used to move the text to the top of the frame
            Button {
                isNight.toggle()
            } label: {
                WeatherButton(title: "Change Day Time",
                              textColor: .blue,
                              backgroundColor: .white)
                
            }
            Spacer()
        }
    }
}

struct weatherDayView: View {
    //this is where i get all the errors like
    //closure expression is unused
    //expected { in struct
    //expressions are not allowed at the top level.
    
    @State var weatherDays: [WeatherDay]
    
    var body: some View {
        ForEach(weatherDays, id: \.self) { singleDay in
            
            VStack {
                Text(singleDay.dayOfWeek)
                    .font(.system(size: 18, weight: .medium, design: .default))
                    .foregroundColor(.white)
                Image(systemName: singleDay.ImageName)
                    .renderingMode(.original)
                    .resizable()
                    .aspectRatio(contentMode: .fit)
                    .frame(width: 40, height: 40)
                Text("\(singleDay.temperature)°")
                    .font(.system(size: 28, weight: .medium))
                    .foregroundColor(.white)
            }
        }
    }
}

【问题讨论】:

    标签: arrays swift foreach swiftui


    【解决方案1】:

    这里有很多错误,从您的ForEach 开始。 ForEach 不是 for...in,尽管它们有类似的目的:循环访问随机访问集合。 ``for...inis used outside of a view, andForEach` 在视图中使用,并为每个元素创建一个视图。

    接下来,你不能像函数一样声明结构。参数进入结构声明内部。之后,这是一个处理视图层次结构的案例,以确保所有内容都具有正确的Type。我已经修复了你的代码,所以你有一个例子,尽管你绝对可以大大收紧你的代码。我强烈建议通过Apple's SwiftUI TutorialsStanford's CS193P course,因为这些是基本的 SwiftUI 问题。

    另外,发布Minimal, Reproducible Example 时要小心。您应该制作一个新的应用程序并确保它可以编译。在本例中,您省略了 WeatherButton() 视图,因此它并不重要,但有时确实如此。

    还有一个简短的说明,通常您不需要在 StackOverflow 中发布您的import,而当您import SwiftUI 时,您会得到由SwiftUI 导入的Foundation,因此您不需要同时导入两者。

    struct ContentView: View {
        @State private var isNight = false
        
        var body: some View {
            ZStack {
                // binding basically enforces that the value for the object stays the same
                BackgroundView(isNight: $isNight)
                VStack {
                    let selectedWeather = getWeatherValues()
                    
                    CityTextView(cityName: selectedWeather.getCity())
                    mainWeatherView(isNight: $isNight, temperature: 76)
                    
                    HStack(spacing: 20) {
                        weatherDayView(weatherDays: selectedWeather.getWeatherDayListData())
                    }
                    
                    
                    
                }
                Spacer() //basically used to move the text to the top of the frame
                Button {
                    isNight.toggle()
                } label: {
                    WeatherButton(title: "Change Day Time",
                                  textColor: .blue,
                                  backgroundColor: .white)
                    
                }
                Spacer()
            }
            
        }
    }
    
    
    struct weatherDayView: View {
    //this is where i get all the errors like
    //closure expression is unused
    //expected { in struct
    //expressions are not allowed at the top level.
            
        @State var weatherDays: [WeatherDay]
        
            var body: some View {
                ForEach(weatherDays, id: \.self) { singleDay in
    
                VStack {
                    Text(singleDay.dayOfWeek)
                        .font(.system(size: 18, weight: .medium, design: .default))
                        .foregroundColor(.white)
                    Image(systemName: singleDay.ImageName)
                        .renderingMode(.original)
                        .resizable()
                        .aspectRatio(contentMode: .fit)
                        .frame(width: 40, height: 40)
                    Text("\(singleDay.temperature)°")
                        .font(.system(size: 28, weight: .medium))
                        .foregroundColor(.white)
                }
            }
        }
    }
    

    编辑:

    添加WeatherDay符合Hashable。

    struct WeatherDay: Hashable {
        var dayOfWeek: String
        var ImageName: String
        var temperature: Int
        
        init(dayOfWeek: String, ImageName: String, temperature: Int)
        {
            self.dayOfWeek = dayOfWeek
            self.ImageName = ImageName
            self.temperature = temperature
            
        }
    }
    

    【讨论】:

    • 我仍然收到错误消息。 Generic struct 'ForEach' requires that 'WeatherDay' conform to 'Hashable' 出现在 forEach 行中。
    • 抱歉,我应该复制所有内容。你现在可以看到WeatherDay。只需使其符合Hashable 协议即可。同样,这是非常基本的,并且在我上面附加的参考资料中有所介绍。
    • 优秀的答案和参考。对于新手问题,我深表歉意。我将研究提到的参考资料。职业生涯中断后很难回到 Dev。基本面似乎很困难和令人沮丧。我从肖恩艾伦的教程开始,而不是从好的旧斯坦福开始。也许我的选择并不明智。
    猜你喜欢
    • 1970-01-01
    • 2018-01-14
    • 2021-05-02
    • 1970-01-01
    • 2018-11-14
    • 2015-05-05
    • 1970-01-01
    • 2013-03-28
    • 1970-01-01
    相关资源
    最近更新 更多