Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 在JSON解析请求中从括号中隔离数据_Swift - Fatal编程技术网

Swift 在JSON解析请求中从括号中隔离数据

Swift 在JSON解析请求中从括号中隔离数据,swift,Swift,我正在测试如何从API获取数据,并获得以下输出: WeatherResponse(temperature: 0.0, humidity: 0.0, pressure: 0.0) 如何将数据单独打印出来,如下所示: 温度:0.0 湿度:0.0 压力:0.0 这是我从操场上得到的代码: import UIKit import PlaygroundSupport guard let url = URL(string: "https://samples.openweathermap.org

我正在测试如何从API获取数据,并获得以下输出:

WeatherResponse(temperature: 0.0, humidity: 0.0, pressure: 0.0)
如何将数据单独打印出来,如下所示:

温度:0.0 湿度:0.0 压力:0.0

这是我从操场上得到的代码:

import UIKit
import PlaygroundSupport

guard let url = URL(string: "https://samples.openweathermap.org/data/2.5/forecast?id=524901&appid=b1b15e88fa797225412429c1c50c122a1") else {
    fatalError("Incorrect URL")
}

struct WeatherResponse: Decodable {
    var temperature: Double = 0
    var humidity: Double = 0
    var pressure: Double = 0
    
    private enum WeatherResponseKeys: String, CodingKey {
        case main
    }
    
    private enum MainKeys: String, CodingKey {
        case temperature = "temp"
        case humidity
        case pressure
    }
    
    init(from decoder: Decoder) throws {
        if let weatherResponseContainer = try? decoder.container(keyedBy: WeatherResponseKeys.self) {
            
            if let mainContainer = try? weatherResponseContainer.nestedContainer(keyedBy: MainKeys.self, forKey: .main) {
                self.temperature = try mainContainer.decode(Double.self, forKey: .temperature)
                self.humidity = try mainContainer.decode(Double.self, forKey: .humidity)
                self.pressure = try mainContainer.decode(Double.self, forKey: .pressure)
            }
        }
    }
}


URLSession.shared.dataTask(with: url) { data, _, _ in
    if let data = data {
        let weatherResponse = try? JSONDecoder().decode(WeatherResponse.self, from: data)
        if let weatherResponse = weatherResponse {
            print(weatherResponse)
        }
    }
} .resume()

您基本上有两种选择:

a) 打印各个组件,如下所示:

print("temperature: \(weatherResponse.temperature) humidity: \(weatherResponse.humidity) pressure: \(weatherResponse.pressure)")
或b),使
天气响应
符合
客户要求
并执行
说明

extension WeatherResponse: CustomStringConvertible {
    var description: String {
       "temperature: \(temperature) humidity: \(humidity) pressure: \(pressure)"
    }
}

您基本上有两种选择:

a) 打印各个组件,如下所示:

print("temperature: \(weatherResponse.temperature) humidity: \(weatherResponse.humidity) pressure: \(weatherResponse.pressure)")
或b),使
天气响应
符合
客户要求
并执行
说明

extension WeatherResponse: CustomStringConvertible {
    var description: String {
       "temperature: \(temperature) humidity: \(humidity) pressure: \(pressure)"
    }
}

谢谢,我将使用第一个:)我的案例现在更加模块化了,顺便说一句,我知道这是不可能的,但是为什么我得到的值是0而不是实际值?不知道。也许值得在一个单独的问题中提问。谢谢,我将使用第一个:)我的案例现在更加模块化了。顺便说一句,我知道这不在问题范围之内,但为什么我在值上得到0而不是实际值?不知道。也许值得单独问一个问题。