无法让 myDescription 值显示在 MainLabel 上,JSON API

Can't get myDescription value to be shown on MainLabel, JSON API

我在 Swift 中为 iOS 制作了一个天气应用程序 4,我正在使用 OpenWeatherMap API 将 JSON 数据解析到应用程序,我有让大部分应用程序正常工作,用户输入一个城市,并显示天气。我有当前温度、当前风速、当前湿度,我正在尝试使当前描述正常工作,但无法显示。

我想让 MainLabel 显示当前的天气描述,但我无法让它工作。代码的顶部是来自 API 键的 JSON 数据,描述在天气部分。我试过 MainLabel.Weather?.Description 但它会打印出完整的描述、ID、Main 和 Icon 值。我将不胜感激。谢谢

这是我用来解码来自 OpenWeatherMap JSON 数据的结构 API:

JSON 结构如下:

import UIKit


//Below is the JSON data from the OpenWeatherMap API


struct Coordinate : Decodable {
    let lat, lon : Double?
}

struct Weather : Decodable {
    var id : Int?
    var main, myDescription, icon : String?

    enum CodingKeys : String, CodingKey {
        case id = "id"
        case main = "main"
        case icon = "icon"
        case myDescription = "description"
    }
}

struct Sys : Decodable {
    let type, id : Int?
    let sunrise, sunset : Date?
    let message : Double?
    let country : String?
}

struct Main : Decodable {
    let temp, tempMin, tempMax : Double?
    let pressure, humidity : Int?
}

struct Wind : Decodable {
    let speed : Double?
    let deg : Int?
}

struct MyWeather : Decodable {
    let coord : Coordinate?
    let cod, visibility, id : Int?
    let name : String?
    let base : String?
    let weather : [Weather]?
    let sys : Sys?
    let main : Main?
    let wind : Wind?
    let dt : Date?
}

查看下面的控制器:

class ViewController: UIViewController {



    @IBOutlet weak var HumidityLabel: UILabel!
    @IBOutlet weak var MainLabel: UILabel!
    @IBOutlet weak var WindLabel: UILabel!
    @IBOutlet weak var TempLabel: UILabel!
    @IBOutlet weak var LocationLabel: UILabel!
    //
    @IBOutlet weak var userValue: UITextField!

    //Assigning Labels




    override func viewDidLoad() {
        super.viewDidLoad()

            }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }




    @IBAction func GoButton(_ sender: Any) {

        let text: String = userValue.text!

        guard let APIUrl = URL(string: "https://api.openweathermap.org/data/2.5/weather?q=" + text +  "&appid=*********APIKEY*********&units=Metric") else { return }
        //API KEY

        URLSession.shared.dataTask(with: APIUrl) { data, response, error in
            guard let data = data else { return }

            let decoder = JSONDecoder()
            //Decoder

            do {
                let weatherData = try decoder.decode(MyWeather.self, from: data)

                if (self.MainLabel != nil)
                {
                    if let gmain = weatherData.weather?.description {
                        print(gmain)
                        DispatchQueue.main.async {
                            self.MainLabel.text! = String (describing: gmain)
                        }
                    }
                }

                if (self.LocationLabel != nil)
                {
                    if let gmain = weatherData.name {
                        print(gmain)
                        DispatchQueue.main.async {
                            self.LocationLabel.text! = "Current Weather in: " + String (gmain)
                        }
                    }
                }


                if (self.HumidityLabel != nil)
                {
                    if let ghumidity = weatherData.main?.humidity
                    {
                        print(ghumidity, "THIS IS HUMIDITY")
                        DispatchQueue.main.async {
                            self.HumidityLabel.text! = String (ghumidity)
                        }
                    }
                }

                if (self.WindLabel != nil)
                {
                    if let gspeed = weatherData.wind?.speed {
                        print(gspeed, "THIS IS THE SPEED")
                        DispatchQueue.main.async {
                            self.WindLabel.text! = String(gspeed) + " mph"
                        }
                    }
                }

                if (self.TempLabel != nil)
                {
                    if let ggtemp = weatherData.main?.temp {
                        print(ggtemp, "THIS IS THE TEMP")
                        DispatchQueue.main.async {
                            self.TempLabel.text! = String (ggtemp) + " c"
                        }
                    }
                }


            } catch {
                print(error.localizedDescription)
            }
            }.resume()

    }

}

通读您的视图控制器代码,您似乎正试图将 weatherData.weather.description 中的值加载到您的标签中,这将是 Weather 对象数组的描述,而不是实际的您从 JSON 响应中解析的值。

查看 Weather 对象,您正在将 JSON 响应中的 'description' 值解析为 myDescription 属性 Weather 对象,Weather 对象放置在 weatherData 对象的数组中。

因此您需要从数组中的对象中获取该值,而不是简单地打印包含 Weather 对象的数组描述。

根据您对问题的描述,我相信这就是您真正想要的(来自 GoButton 函数):

if (self.MainLabel != nil)
{
    // get the first weather object in the 'weatherData's weather array, and
    // use the description parsed from the json and saved in the 'myDescription'
    // property here...
    if let gmain = weatherData.weather?.first?.myDescription {
        print(gmain)
        DispatchQueue.main.async {
            self.MainLabel.text! = String (describing: gmain)
        }
    }
}