Hello, I'm using WeatherKit in a project to display the SF Symbol for the weather in the app toolbar. I have this for the WeatherModel:
import Foundation
import WeatherKit
@MainActor class WeatherManager: ObservableObject {
@Published var weather: Weather?
public func getWeather() async {
do {
weather = try await Task.detached(priority: .userInitiated) {
return try await WeatherService.shared.weather(for: .init(latitude: 28.3772, longitude: -81.5707))
}.value
} catch {
fatalError("\(error)")
}
}
var symbol: String {
weather?.currentWeather.symbolName ?? "xmark.icloud.fill"
}
var temp: String {
let temp =
weather?.currentWeather.temperature
let convert = temp?.converted(to: .fahrenheit).description
return convert ?? ""
}
}
And this in the SwiftUI View:
ToolbarItem(placement: .topBarLeading) {
@ObservedObject var weatherManager = WeatherManager()
Image(systemName: weatherManager.symbol)
.task {
await weatherManager.getWeather()
}
}
But when I build and run the app on my phone it displays the "xmark.icloud.fill"
instead of the actual weather.
Did I do something wrong?