SwiftUI Weather App with OpenWeatherMap API returns "Failed to log metrics" 错误

SwiftUI Weather App with OpenWeatherMap API returns "Failed to log metrics" error

我正在尝试设置一个新的 SwiftUI 天气应用程序,它使用 OpenWeatherMap Api 字符串 "api.openweathermap.org/data/2.5/weather?q={city name}&appid ={API key}" 传入一个城市。我将我的 contentView、模型和 viewModel 设置为 return 城市名称、温度和天气描述,但是当我 运行 应用程序时仍然出现以下错误:

2021-11-09 23:26:56.923188-0500 SwftUIMVVMWeatherDemo[13982:634181] [boringssl] boringssl_metrics_log_metric_block_invoke(144) Failed to log metrics
failed

我的contentView、model、viewModel如下:

内容视图

import SwiftUI

struct ContentView: View {
    
    @StateObject var viewModel = WeatherViewModel()
        
    var body: some View {
        NavigationView {

            VStack {
                
                Text(viewModel.name)
                    .font(.system(size: 32))
                Text(viewModel.temp)
                    .font(.system(size: 44))
                Text(viewModel.descriptionText)
                    .font(.system(size: 24))

                Spacer()
            }
            .navigationTitle("Weather MVVM")
        }.padding()
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

型号

import Foundation

struct WeatherModel: Codable {
    let name: String
    let main: CurrentWeather
}

struct CurrentWeather: Codable {
    let temp: Float
    let weather: [WeatherInfo]
}

struct WeatherInfo: Codable {
    let description: String
}

视图模型

import Foundation

class WeatherViewModel: ObservableObject {
    
    @Published var name: String = "-"
    @Published var temp: String = "-"
    @Published var descriptionText: String = "-"
    
    init() {
        fetchWeather()
    }
    
    func fetchWeather() {
                
        guard let url = URL(string: "https://api.openweathermap.org/data/2.5/weather?q=chicago&appid=<myAPIKeyGoesHere>") else {
            return
        }
        
        let task = URLSession.shared.dataTask(with: url) { data, _, error in
            // get data
            guard let data = data, error == nil else {
                return
            }
            
            //convert data to model
            do {
                let model = try JSONDecoder().decode(WeatherModel.self, from: data)
                
                DispatchQueue.main.async {                    
                    self.name = model.name
                    self.temp = "\(model.main.temp)"
                    self.descriptionText = model.main.weather.first?.description ?? "No Description"
                }
            }
            catch {
                print("failed")
            }
        }
        task.resume()
    }
}

错误信息含糊不清。知道可能出什么问题了吗?感谢反馈!

可能是您的型号不正确。试试这个:

struct WeatherModel: Codable {
    let name: String
    let main: CurrentWeather
    let weather: [WeatherInfo]   // <--- here
}

struct CurrentWeather: Codable {
    let temp: Float
    // let weather: [WeatherInfo]  // <--- NOT here
}

城市也是 Chicago,虽然它似乎适用于 chicago