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
Answered by Davu in 753142022

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
    }
}
Accepted Answer

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
    }
}
Following "Meet WeatherKit"- getting async error
 
 
Q