iosswiftweather-apiweatherkit

WeatherKit - currentWeather autoupdate


Will the var WeatherKit.Weather.currentWeather auto-update itself, as time passes by, based on the daily and hourly forecast? Or it's a static value, once received stays constant?


Solution

  • It is a static value. It does not automatically update.

    The signature for currentWeather in the documentation shows that it is a simple stored property in the Weather struct.

    var currentWeather: CurrentWeather
    

    It is lacking the { get } or { get set } accessor declaration, which only a computed property would have. Compare this to EnvironmentValues.dismiss and Environment.controlSize for example.

    Since it is a stored property in a struct, there is nothing fancy it can do in its getters/setters, and other code cannot possibly modify it without you passing it to them "by reference", since it is just a value on your stack.


    Plus, since this is a var, not a let, you can actually set it to another value. It doesn't make sense to allow you to do this if the value is auto-updating, does it?

    func f(x: inout Weather, y: CurrentWeather) {
        x.currentWeather = y // this compiles!
    }