所以這就是問題所在。當我在檔案中給出值時,我的代碼運行良好。但是,我制作了一個單獨的模型檔案,我想從中遍歷 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)
}
}
}
}
uj5u.com熱心網友回復:
這里有一堆錯誤,從你的ForEach. ForEach不是for...in,盡管它們具有相似的目的:回圈遍歷隨機訪問集合。``for...in is used outside of a view, andForEach` 用于視圖中,并為每個元素創建一個視圖。
接下來,您不能像函式一樣宣告結構體。引數位于結構宣告中。之后是處理視圖層次結構以確保所有內容都正確的情況Type。我已經修復了你的代碼,所以你有一個例子,盡管你絕對可以大大收緊你的代碼。我強烈建議您閱讀Apple 的 SwiftUI 教程和斯坦福的 CS193P 課程,因為這些是基本的 SwiftUI 問題。
另外,發布最小的、可重現的示例時要小心。您應該制作一個新的應用程式并確保它可以編譯。在這種情況下,您忽略了WeatherButton()視圖,因此它并不重要,但有時確實如此。
還有一個簡短的說明,通常您不需要import在 StackOverflow 中發布您的內容,并且當import SwiftUI您通過Foundation匯入時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
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/377986.html
上一篇:在Javascript中過濾(搜索)兩個值(搜索條件)
下一篇:用Java從陣列中洗掉元素
