不能在 swift 中使用 for...in 或 forEach 循环打印 swift 中的视图
cannot use for...in or forEach loop in swift to print views in 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
,尽管它们具有相似的目的:循环遍历随机访问集合。 ``for...inis used outside of a view, and
ForEach` 在视图中使用,并为每个元素创建一个视图。
接下来,您不能像函数一样声明结构。参数位于结构声明中。之后是处理视图层次结构以确保所有内容都正确 Type
的情况。我已经修复了你的代码,所以你有一个例子,尽管你肯定可以大大加强你的代码。我强烈建议您完成 Apple's SwiftUI Tutorials and Stanford's CS193P course,因为这些是基本的 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)
}
}
}
}
编辑:
已添加 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
}
}
问题来了。当我在文件中给出值时,我的代码 运行 没问题。但是,我制作了一个单独的模型文件,我想从中迭代 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
,尽管它们具有相似的目的:循环遍历随机访问集合。 ``for...inis used outside of a view, and
ForEach` 在视图中使用,并为每个元素创建一个视图。
接下来,您不能像函数一样声明结构。参数位于结构声明中。之后是处理视图层次结构以确保所有内容都正确 Type
的情况。我已经修复了你的代码,所以你有一个例子,尽管你肯定可以大大加强你的代码。我强烈建议您完成 Apple's SwiftUI Tutorials and Stanford's CS193P course,因为这些是基本的 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)
}
}
}
}
编辑:
已添加 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
}
}