使用SwiftyJSON进行JSON解析-无法更新UI

时间:2019-01-16 21:11:55

标签: ios json swift swifty-json

我编写了以下代码,该代码从Dark Sky API中提取数据。我正在使用SwiftyJSON对其进行解析,但随后无法获得UI标签“ wind”来显示风速。

我认为错误可能出在我的解析中。我已经使用JSON编码器找到了我想要提取的参数,即windSpeed,但是我不知道这是我弄错了还是在更新UI本身。当我执行API的get请求时,我还获得了该请求的多个实例,所以问题可能也出在那儿?

我的代码如下:

import UIKit
import CoreLocation
import Alamofire
import SwiftyJSON

class ViewController: UIViewController, CLLocationManagerDelegate {

    let base_URL = "https://api.darksky.net/forecast/[API Key here]/"

    //Instance variable
    let locationManager = CLLocationManager()
    let windDataModel = WindDataModel()

    @IBOutlet weak var windDirectionArrow: UIImageView!
    @IBOutlet weak var yard: UILabel!
    @IBOutlet weak var gust: UILabel!
    @IBOutlet weak var wind: UILabel!


    override func viewDidLoad() {
        super.viewDidLoad()
        //Location manager set up
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestWhenInUseAuthorization()
        locationManager.startUpdatingLocation()
    }

    //Get wind data method
    func getWindData(url: String, latitude: String, longitude: String) {
        let urlStr = "\(base_URL)\(latitude),\(longitude)"
        Alamofire.request(urlStr, method: .get, parameters:nil, encoding: JSONEncoding.default).responseJSON { [weak self] response in
            if response.result.isSuccess {
                print("Success! Got the weather data")
                let windJSON : JSON = JSON(response.result.value!)
                print(windJSON)
                self!.updateWindData (json: windJSON)
            }  else {
                print("Error \(String(describing: response.result.error))") }
            self?.wind.text = "Connection issues"
        }
    }

    //MARK: - JSON Parsing
    /***************************************************************/
    //
    //    //Write the updateWeatherData method here:
    func updateWindData(json: JSON) {
        let windSpeed = json["currently"]["windSpeed"].doubleValue
        windDataModel.speed = Double(windSpeed)
        updateUIWithWindData()
    }

    ////    //Write the updateUIWithWeatherData method here:
    func updateUIWithWindData() {
        wind.text = "\(windDataModel.speed)"
        //Did update method
        func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
            let location = locations[locations.count - 1]
            if location.horizontalAccuracy > 0 {
                self.locationManager.stopUpdatingLocation()
                self.locationManager.delegate = nil

                let latitude = String(location.coordinate.latitude)
                let longitude = String(location.coordinate.longitude)

                getWindData(url: base_URL, latitude: latitude, longitude: longitude)
            }
        }

        //Did fail with error method
        func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
            print(error)
            wind.text = "Error"
        }

    }
}

1 个答案:

答案 0 :(得分:2)

您所有的括号都不合适。

func updateUIWithWindData() {

  wind.text = "\(windDataModel.speed)"

此功能之后,将结束括号放在wind.text = "...之后,您将结束括号放在类的底部附近。

另外,使用else语句还有另外一个括号:

else {
  print("Error \(String(describing: response.result.error))") }
  self?.wind.text = "Connection issues"

应将print("Error...行末的括号移到"Connection issues"之后。

修复这些问题,您的代码应该可以正常工作。