我的天气数据未显示在视图控制器中

时间:2018-07-11 18:07:40

标签: ios swift xcode

即使我可以在输出窗口中看到数据,我也不知道为什么视图控制器没有显示数据。

输出:

Muḩāfaz̧at Al Jīzah

Clear

88.0

我的代码:

override func viewDidLoad() {
    super.viewDidLoad()

    loadCurrentWeather = currentWeatherData()
    loadCurrentWeather.downloadWeatherData {
        //setting uo UI to download data
        self.updateTodayUI()
    }
}

func updateTodayUI() {
    locationLabel.text = loadCurrentWeather.cityName
    weatherTypeLabel.text = loadCurrentWeather.weatherType
    currentTempLabel.text = "\(loadCurrentWeather.currentTemp)"
    weatherTypeImage.image = UIImage(named: loadCurrentWeather.weatherType)
}

我在Xcode中的视图控制器:

screen in xcode

我在iPhone上的视图控制器:

screen on iphone

currentweatherData是我下载数​​据表单的代码。

import UIKit
import Alamofire

class currentWeatherData {
    var cityNameone: String!
    var dateone: String!
    var weatherTypeone: String!
    var currentTempone: Double!

    var cityName: String {
        if cityNameone == nil {
            cityNameone = ""
        }
        return cityNameone
    }

    var date: String {
        if dateone == nil {
            dateone = ""
        }
        let dateFormatter = DateFormatter()
        dateFormatter.dateStyle = .short
        dateFormatter.timeStyle = .none
        let currentDate = dateFormatter.string(from: Date())
        self.dateone = "Today, \(currentDate)"
        return dateone
    }

    var weatherType: String{
        if weatherTypeone == nil{
            weatherTypeone = ""
        }
        return weatherTypeone
    }

    var currentTemp: Double {
        if currentTempone == nil {
            currentTempone = 0.0
        }
        return currentTempone
    }

    func downloadWeatherData(completed: DownloadComplete){
        // to tell alamofire where to download the data
        let weatherURL = URL (string: currentWeatherURL)!
        Alamofire.request(weatherURL).responseJSON{ response in
            let result = response.result

            if let dictionary = result.value as? Dictionary<String, AnyObject>{
                if let name = dictionary["name"] as? String {
                    self.cityNameone = name.capitalized
                    print(self.cityNameone ?? "No city name")
                }

                if let weather = dictionary["weather"] as? [Dictionary<String, AnyObject>]{
                    if let main = weather[0]["main"] as? String {
                        self.weatherTypeone = main.capitalized
                        print(self.weatherTypeone ?? "No weather type")
                    }
                }

                if let main = dictionary["main"] as? Dictionary<String, AnyObject> {
                    if let currentTemperature = main["temp"] as? Double {
                        let kelvintoFarenheit = (currentTemperature * (9/5) - 459.67)
                        let totalKelvinToFarenheit = Double(round(10 * kelvintoFarenheit/10))
                        self.currentTempone = totalKelvinToFarenheit
                        print(self.currentTempone ?? .nan)

                    }
                }
            }
        }

        completed()
    }
}

我的代码或视图控制器是否存在问题?我的约束有问题吗?

我似乎无法弄清楚。

2 个答案:

答案 0 :(得分:0)

我看不到您要解决的所有代码,但是您可能会遇到并发问题。尝试将对updateTodayUI内的viewDidLoad(_:)的调用放在异步块内,如下所示:

DispatchQueue.main.async {
    updateTodayUI()
}

您可以在documentation中找到有关调度队列和并发性的更多信息。

答案 1 :(得分:0)

您正在调用completed太早-在JSON响应到达之前。您必须在responseJSON调用的闭包内调用它:

Alamofire.request(weatherURL).responseJSON { response in
    let result = response.result
    // ...
    completed()
}