gpt4 book ai didi

json - OpenWeatherMap 中的字符串值和天气图标

转载 作者:行者123 更新时间:2023-11-30 10:45:24 26 4
gpt4 key购买 nike

我试图从值中检索 Main 和 Description,但它没有显示在 UILabel 中。所有其他参数都显示良好。如温度、最高和最低温度、风速和风向。我在使用天气图标时也遇到困难,如果我的代码正确,则需要一些指导。

import UIKit
import Foundation

class ViewController: UIViewController {
var timer = Timer()

struct CurrentLocalWeather: Codable {
let base: String
let clouds: Clouds
let cod: Int
let coord: Coord
let dt: Int
let id: Int
let main: Main
let name: String
let sys: Sys
let visibility: Int
let weather: Weather
let wind: Wind
}

struct Clouds: Codable {
let all: Int
}

struct Coord: Codable {
let lat: Double
let lon: Double
}

struct Main: Codable {
let humidity: Int
let pressure: Int
let temp: Double
let tempMax: Double
let tempMin: Double
private enum CodingKeys: String, CodingKey {
case humidity, pressure, temp, tempMax = "temp_max", tempMin = "temp_min"
}
}

struct Sys: Codable {
let country: String
let id: Int
let message: Double
let sunrise: UInt64
let sunset: UInt64
let type: Int
}

struct Weather: Codable {
let description: String
let icon: String
let id: Int
let main: String
}

struct Wind: Codable {
let deg: Int
let speed: Double
}

//Variables from Storyboard
@IBOutlet weak var Temp: UILabel!
@IBOutlet weak var TempMin: UILabel!
@IBOutlet weak var TempMax: UILabel!
@IBOutlet weak var WindSpeed: UILabel!
@IBOutlet weak var Direction: UILabel!
@IBOutlet weak var Humidity: UILabel!
@IBOutlet weak var MainDesc: UILabel!
@IBOutlet weak var Description: UILabel!
@IBOutlet weak var City: UILabel!
@IBOutlet weak var SunRise: UILabel!
@IBOutlet weak var SunSet: UILabel!
@IBOutlet weak var Icon: UIImageView!

override func viewDidLoad() {
super.viewDidLoad()

// Define URL for Weather Data
URLSession.shared.dataTask(with: URL(string: "https://api.openweathermap.org/data/2.5/weather?lat=51.05011&lon=-114.08529&appid=<APPID_REMOVED_FOR_POSTING_PURPOSE>&units=metric")!) { data, response, error in
if let error = error {
print("Error:\n\(error)")
} else {
do {
let JSon = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as! [String: AnyObject]

if let Temp1 = JSon["weather"]as? NSDictionary {
DispatchQueue.main.async {
self.Description.text = Temp1["description"] as? String
self.MainDesc.text = Temp1["main"] as? String
let Ico = (Temp1["icon"] as? String)!
let Icon = UIImageView()
let urlStr = NSURL(string:"http://openweathermap.org/img/w/\(Ico).png")
let urlData = NSData(contentsOf: urlStr! as URL)
if urlData == nil {
Icon.image = UIImage(data: urlData! as Data)
}
}
}
} catch let jsonError as NSError {
print("JSON error:\n\(jsonError.description)")
}
}
}.resume()
}

这是查询的结果:

{"coord":{"lon":-114.09,"lat":51.05},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03d"}],"base":"stations","main":{"temp":10.9,"pressure":1014,"humidity":32,"temp_min":8.89,"temp_max":13.33},"visibility":56326,"wind":{"speed":11.3,"deg":20},"clouds":{"all":40},"dt":1556142366,"sys":{"type":1,"id":989,"message":0.0068,"country":"CA","sunrise":1556108578,"sunset":1556160342},"id":5913490,"name":"Calgary","cod":200}

最佳答案

通过进行以下更改,我能够解决字符串值未显示的问题。

Import UIKit
import Foundation

class ViewController: UIViewController {
var timer = Timer()


//Variables from Storyboard
@IBOutlet weak var Temp: UILabel!
@IBOutlet weak var TempMin: UILabel!
@IBOutlet weak var TempMax: UILabel!
@IBOutlet weak var WindSpeed: UILabel!
@IBOutlet weak var Direction: UILabel!
@IBOutlet weak var Humidity: UILabel!
@IBOutlet weak var MainDesc: UILabel!
@IBOutlet weak var Description: UILabel!
@IBOutlet weak var City: UILabel!
@IBOutlet weak var SunRise: UILabel!
@IBOutlet weak var SunSet: UILabel!
@IBOutlet weak var Icon: UIImageView!

override func viewDidLoad() {
super.viewDidLoad()

// Define URL for Weather Data
URLSession.shared.dataTask(with: URL(string: "https://api.openweathermap.org/data/2.5/weather?lat=51.05011&lon=-114.08529&appid=<APPID_REMOVED_FOR_POSTING_PURPOSE>&units=metric")!) { data, response, error in
if let error = error {
print("Error:\n\(error)")
} else {
do {
let JSon = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as! [String: AnyObject]

let weatherData = (JSon["weather"]as! NSArray)
if let Temp1 = weatherData[0] as? NSDictionary {
DispatchQueue.main.async {
self.Description.text = Temp1["description"] as? String
self.MainDesc.text = Temp1["main"] as? String
let Ico = (Temp1["icon"] as? String)!
let urlStr = NSURL(string:"http://openweathermap.org/img/w/\(Ico).png")
let urlData = NSData(contentsOf: urlStr! as URL)
if urlData != nil {
self.Icon.image = UIImage(data: urlData! as Data)
}
}
}
} catch let jsonError as NSError {
print("JSON error:\n\(jsonError.description)")
}
}
}.resume()
}

我仍然坚持让图标正常工作,希望我也能发布它。对于尝试使用 Codable 并正确使用的人,请分享。

关于json - OpenWeatherMap 中的字符串值和天气图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55839115/

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