我正在尝试使用AlamoFire构建天气应用程序,并且按预期获得JSON响应,但是当我尝试将日期更新为UI Label时,我的变量返回nil。如果有人有机会让我知道我要去哪里错了
import UIKit
class WeatherVC: UIViewController {
@IBOutlet weak var dateLbl: UILabel!
@IBOutlet weak var weatherType: UILabel!
@IBOutlet weak var cityname: UILabel!
@IBOutlet weak var currentTemp: UILabel!
var weatherConstants = WeatherConstant()
override func viewDidLoad() {
super.viewDidLoad()
// updateUI()
//
weatherConstants.downloadCurrentWeather {
self.updateUI()
}
weatherConstants = WeatherConstant()
print(Current_Weather_Url)
}
func updateUI() {
weatherType.text = weatherConstants.weatherType
cityname.text = weatherConstants.cityName
print("current city name is \(weatherConstants.weatherType)")
}
}
天气常数
import UIKit
import Alamofire
class WeatherConstant {
var _cityName :String!
var _currentTemp : Double!
var _weatherType : String!
var _highTemp : String!
var _date :String!
var cityName :String {
if _cityName == nil {
_cityName = ""
}
return _cityName
}
var currentTemp : Double{
if _currentTemp == nil {
_currentTemp = 0.0
}
return _currentTemp
}
var weatherType : String {
if _weatherType == nil {
_weatherType = ""
}
return _weatherType
}
var highTemp : String {
if _highTemp == nil {
_highTemp = ""
}
return _highTemp
}
var date : String {
if _date == nil {
_date = ""
}
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .long
dateFormatter.timeStyle = .none
let currentDate = dateFormatter.string(from: Date())
self._date = "Today, \(currentDate)"
return _date
}
//func downloadWeatherDetails(completed : DownloadComplete) ===> downloadWeatherDetails(completed : @escaping DownloadComplete)
func downloadCurrentWeather(completed: @escaping DownloadComplete) {
Alamofire.request(Current_Weather_Url).responseJSON{ response in
let result = response.result
// print(result)
debugPrint(result)
if let dict = result.value as? Dictionary<String, AnyObject>{
if let name = dict["name"] as? String {
self._cityName = name
print("current name is \(self._cityName)")
}
if let currentTemp = dict["weather"] as? [Dictionary<String, AnyObject>] {
if let main = currentTemp[0]["main"] as? String{
self._weatherType = main
print("Current Weather Type \(self._weatherType)")
}
}
}
completed()
}
}
}
JSON响应
https://samples.openweathermap.org/data/2.5/weather?lat=35&lon=139&appid=e9a34088739f38c517b4a45084f5ed82
SUCCESS: {
base = stations;
clouds = {
all = 0;
};
cod = 200;
coord = {
lat = "35.02";
lon = "139.01";
};
dt = 1485792967;
id = 1907296;
main = {
"grnd_level" = "1013.75";
humidity = 100;
pressure = "1013.75";
"sea_level" = "1023.22";
temp = "285.514";
"temp_max" = "285.514";
"temp_min" = "285.514";
};
name = Tawarano;
sys = {
country = JP;
message = "0.0025";
sunrise = 1485726240;
sunset = 1485763863;
};
weather = (
{
description = "clear sky";
icon = 01n;
id = 800;
main = Clear;
}
);
wind = {
deg = 311;
speed = "5.52";
};
}
当前天气类型Optional(“清除”)---->这是我要提供给UIlabel的天气类型
当前城市名称为Optional(“”)------>在这里,我尝试打印UILabel中的数据,但返回空字符串
2018-08-07 11:18:09.567868-0700 WeatherApp1.1 [82321:3248301] [BoringSSL]函数boringssl_session_errorlog:第2881行[boringssl_session_read] SSL_ERROR_ZERO_RETURN(6):操作失败,因为连接已被干净关闭close_notify警报
答案 0 :(得分:0)
您正在weatherConstants
中用新对象覆盖viewDidLoad
值。尝试删除它。
(更长的解释:您正在正确创建一个weatherConstants
对象,并要求它获取数据。但是,在这种情况下,您创建了该对象的第二个实例,该实例将覆盖第一个实例。因此,第一个实例完成网络请求并告诉您的UI重绘,该UI正在从WeatherConstants
)的 second 实例中获取其数据