JSON 使用 Swifty 解析JSON - 更新时遇到问题 UI
JSON parsing with SwiftyJSON - Trouble updating UI
我编写了以下代码,从 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"
}
}
}
你有一些牙套不合适就够了。
func updateUIWithWindData() {
wind.text = "\(windDataModel.speed)"
在此函数之后,将结束大括号紧跟在 wind.text = "...
之后。结束大括号靠近 class 的底部。
此外,您还有另一个大括号与此 else 语句不符:
else {
print("Error \(String(describing: response.result.error))") }
self?.wind.text = "Connection issues"
print("Error...
行末尾的大括号应移到 "Connection issues"
之后。
修复这些问题,您的代码应该可以正常工作。
我编写了以下代码,从 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"
}
}
}
你有一些牙套不合适就够了。
func updateUIWithWindData() {
wind.text = "\(windDataModel.speed)"
在此函数之后,将结束大括号紧跟在 wind.text = "...
之后。结束大括号靠近 class 的底部。
此外,您还有另一个大括号与此 else 语句不符:
else {
print("Error \(String(describing: response.result.error))") }
self?.wind.text = "Connection issues"
print("Error...
行末尾的大括号应移到 "Connection issues"
之后。
修复这些问题,您的代码应该可以正常工作。