我试图理解为什么它Task{} 有效的原因,但是当我这样做时,VStack{}.task{}我收到错误:
从“@Sendable () async throws -> ()”类型的抛出函数到非抛出函数类型“@Sendable () async -> Void”的转换无效
我试图让该fetchWeather()函数在视图启动时运行,而无需用户点击按钮。(如果下面的方法是正确的方法)但是遇到了这个错误并且我真的很好奇这个错误背后的原因
下面是我的代码:
struct WeatherView: View {
var body: some View {
VStack{
Text("Hello, World!")
Button("Get Weather", action: {
// Works when I tap the button
// Task {
// try await fetchWeather()
// }
})
}
//*******Xcode error with invalid conversion*******
.task {
try await fetchWeather()
}
}
}
struct WeatherView_Previews: PreviewProvider {
static var previews: some View {
WeatherView( )
}
} …Run Code Online (Sandbox Code Playgroud)