swiftswiftuiweatherkit

Create an example WeatherAlert object


I'm working with WeatherKit which has a struct called WeatherAlert. WeatherAlert has an init method that requires a Decoder:

/// - Parameter decoder: The decoder to read data from.
    public init(from decoder: Decoder) throws

I'd like to create an example WeatherAlert to use in a view preview. But I'm at a loss for how to create an object using the method above. How can I create an example WeatherKit object for use in my preview?

#Preview {
    
    //??

    let testAlert: WeatherAlert = WeatherAlert(from: ??)
    
    AlertView(alert: testAlert)
}


Solution

  • You can get a JSON from WeatherKit since WeatherAlert is Codable

    let forecast = try await weatherService.weather(for: location, including: .alerts)
    let data = try JSONEncoder().encode(forecast) //Make the JSON
    print(String(data: data, encoding: .utf8) as AnyObject) //Prints a string to console
    

    Once you have a JSON you can put it in a .json file and load it when you need it.

    func load<T: Decodable>(_ filename: String) -> T {
        let data: Data
    
        guard let file = Bundle.main.url(forResource: filename, withExtension: nil)
            else {
                fatalError("Couldn't find \(filename) in main bundle.")
        }
    
        do {
            data = try Data(contentsOf: file)
        } catch {
            fatalError("Couldn't load \(filename) from main bundle:\n\(error)")
        }
    
        do {
            let decoder = JSONDecoder()
            return try decoder.decode(T.self, from: data)
        } catch {
            fatalError("Couldn't parse \(filename) as \(T.self):\n\(error)")
        }
    }
    

    The code will look something like

    let alert: [WeatherAlert] = load("WeatherAlerts.json")
    

    The trick with alert is that you need to get a sample JSON from a place that currently has alerts. So it might help to look at a place on the weather map and use that location.