I'd like to "unwrap" the sunrise
from dayWeather
but I don't know what I use instead of "Something". I've tried many things without success. Xcode always tells me that "dayWeather is inaccessible". Do you know what can I use?
extension "Something" {
public var unwrappedSunrise: Date {
dayWeather.first?.sun.sunrise ?? Date()
}
}
Here's the WeatherManager
I've got from a nice dude on SO.
@MainActor
final class WeatherManager: ObservableObject {
enum LoadingState {
case idle
case loading
case loaded(CurrentWeather, Forecast<DayWeather>)
case failed(Error)
}
@Published var loadingState: LoadingState = .idle
init() {
loadingState = .loading
Task {
await requestWeather()
}
}
func requestWeather() async {
let location = CLLocation(latitude: 48.856613, longitude: 2.352222) /* Paris, France */
let nowDate = Date.now
let sevenDaysFromDate = Calendar.current.date(byAdding: .day, value: 7, to: nowDate) ?? Date()
let currentWeather: WeatherQuery = .current
let dailyWeather: WeatherQuery = .daily(startDate: nowDate, endDate: sevenDaysFromDate)
do {
let (currentWeather, dayWeather) = try await WeatherService.shared.weather(for: location, including: currentWeather, dailyWeather)
loadingState = .loaded(currentWeather, dayWeather)
} catch {
loadingState = .failed(error)
}
}
}
Thanks, in advance!
You could try something like this:
and call it like this
but as mentioned this is hidden code, you are better off using
then you know exactly what your code is doing.
You could also use a function (eg in your WeatherManager) such as:
and call it like this inside
WeatherManager
:or, outside