不能用于..in或for swift中的每个循环都可以在swift中打印视图



所以这就是问题所在。当我在文件中给定值时,我的代码运行良好。然而,我制作了一个单独的模型文件,我想从中迭代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)
}
}
}
}

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

接下来,不能像函数一样声明结构。参数位于结构声明内部。之后是一个处理视图层次结构的案例,确保所有内容都具有正确的Type。我已经修复了你的代码,所以你有一个例子,尽管你肯定可以大量收紧你的代码。我强烈建议你学习苹果的SwiftUI教程和斯坦福大学的CS193P课程,因为这些都是SwiftUI的基本问题。

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)
}
}
}
}

编辑:

添加了符合Hashable的WeatherDay

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

}
}

最新更新