2016-11-13 55 views
-1

這是我想獲得執行後,下載所有細節和另一個功能,其中下載後更新UIElements的功能。此功能是內部 「CurrentWeather.swift」執行功能後,功能不工作迅速3

func downloadWeatherDetails(completed: (() ->())){ 
    // tell Alimofire where to download 
    let currentWeatherURL = URL(string: CURRENT_WEATHER_URL)! 
    Alamofire.request(currentWeatherURL).responseJSON{ response in 

     let result = response.result 
     if let dict = result.value as? Dictionary<String, AnyObject>{ 

      if let name = dict["name"] as? String{ 
       self._cityName = name.capitalized 
       print(self._cityName) 
      } 

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

      if let main = dict["main"] as? Dictionary<String, AnyObject>{ 
       if let currentTemperature = main["temp"] as? Double { 
        let kelvinToFarenhitePreDevision = (currentTemperature * (9/5) - 459.67) 
        let kelvinToFarenhite = Double(round(10 * kelvinToFarenhitePreDevision/10)) 
        self._currentTemp = kelvinToFarenhite 
        print(self._currentTemp) 
       } 
      } 


     } 


    } 
    completed() 
} 

}

而這個函數是更新UIEmelents

func updateMainUI(){ 

    dateLabel.text = currentWeather.date 
    currentTempLabel.text = "\(currentWeather.currentTemp)" 
    currentWeatherTypeLabel.text = currentWeather.weatherType 
    locationLabel.text = currentWeather.cityName 
    currentWeatherImage.image = UIImage(named: currentWeather.weatherType) 
} 

,這是給他們打電話上覆蓋FUNC viewDidLoad中()

override func viewDidLoad() { 
    super.viewDidLoad() 
    tableView.delegate = self 
    tableView.dataSource = self 

    currentWeather.downloadWeatherDetails {() ->() in 
     self.updateMainUI() 
    } 
} 

現在,當我執行這個它不更新UI數據 b UT斯達康公司將它們打印內部控制檯

任何人可以幫助我這個問題

回答

2

要調用completed()完成您的請求之前。

 completed() //<- you need to call the completion handler inside the closure. 
    } 
    //completed() //<- this would be called before completion 
} 

我還沒有檢查過你的代碼的其他部分,但至少你需要解決這個問題。

+0

Thrick bro 它的工作 –

0

您的weatherDetails正在異步下載。在viewDidLoad()中,在接收並分析響應之前調用self.updateMainUI()。所以只是把UIUpdateMainUI()方法中,並在該塊的結尾:

if let dict = result.value as? Dictionary<String, AnyObject>{ 
} 

如果從不同的類要求,然後設計一個協議存在。在你想要更新的viewController中使用它。從上面的if-let塊中,在解析結束時,調用viewController的委託方法進行更新。當然,在委託方法中,更新您的用戶界面。 謝謝。