gpt4 book ai didi

ios - 无法让 myDescription 值显示在 MainLabel、JSON API 上

转载 作者:行者123 更新时间:2023-11-30 11:14:59 25 4
gpt4 key购买 nike

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

我想让 MainLabel 显示当前的天气描述,但我无法让它工作。代码顶部是来自 API key 的 JSON 数据,描述位于天气部分。我尝试过 MainLabel.Weather?.Description 但它打印出整个描述、ID、Main 和 Icon 值。我将不胜感激对此的指导。谢谢您

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

以下 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?
}

查看下面的 Controller :

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()

}

}

最佳答案

仔细阅读您的 View Controller 代码,您似乎正在尝试将 weatherData.weather.description 中的值加载到标签中,该标签将是 Weather< 数组的描述 对象,而不是您从 JSON 响应解析的实际值。

查看 Weather 对象,您将 JSON 响应中的“description”值解析到 WeathermyDescription 属性中对象,并且 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)
}
}
}

关于ios - 无法让 myDescription 值显示在 MainLabel、JSON API 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51830617/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com