Following "Meet WeatherKit"- getting async error

'async' call cannot occur in a global variable initializer for weather variable.

import WeatherKit
import CoreLocation

let weatherService = WeatherService()
let chicago = CLLocation(latitude: 47, longitude: 87.58293)

let weather = try! await weatherService.weather(for: chicago)
let temperature = weather.currentWeather.temperature

Accepted Reply

Solved the issue: I forgot to include the given code in a struct and initialize the variables.

import WeatherKit
import CoreLocation

struct RequestWeather {
    let weatherService = WeatherService()
    let chicago = CLLocation(latitude: 47, longitude: 87.58293)

    let weather : Weather
    let temperature : Measurement<UnitTemperature>

    init() async {
        weather = try! await weatherService.weather(for: chicago)
        temperature = weather.currentWeather.temperature
    }
}

Replies

Solved the issue: I forgot to include the given code in a struct and initialize the variables.

import WeatherKit
import CoreLocation

struct RequestWeather {
    let weatherService = WeatherService()
    let chicago = CLLocation(latitude: 47, longitude: 87.58293)

    let weather : Weather
    let temperature : Measurement<UnitTemperature>

    init() async {
        weather = try! await weatherService.weather(for: chicago)
        temperature = weather.currentWeather.temperature
    }
}