【发布时间】:2015-09-30 15:54:01
【问题描述】:
我在 swift 2 上遇到了一个错误,它很简单但我无法理解
这是我开始为 Swift 2 更新之前的代码:
func updateWeatherInfo(latitude: CLLocationDegrees, longitude: CLLocationDegrees) {
let url = "http://api.openweathermap.org/data/2.5/forecast"
let params = ["lat":latitude, "lon":longitude]
println(params)
Alamofire.request(.GET, url, parameters: params)
.responseJSON { (request, response, json, error) in
if(error != nil) {
println("Error: \(error)")
println(request)
println(response)
self.loading.text = "Internet appears down!"
}
else {
println("Success: \(url)")
println(request)
var json = JSON(json!)
self.updateUISuccess(json)
}
}
}
这里是抛线错误:
let json = JSON(json)
这是我开始后的全部功能
func updateWeatherInfo(latitude: CLLocationDegrees, longitude: CLLocationDegrees) {
let url = "http://api.openweathermap.org/data/2.5/forecast"
let params = ["lat":latitude, "lon":longitude]
print(params)
Alamofire.request(.GET, url, parameters: params)
.responseJSON { (request, response, json) in
print("Success: \(url)")
print(request)
let json = JSON(json)
self.updateUISuccess(json!)
}
}
updateUISuccess 需要一个 JSON 值,但目前我有一个 (Request)
经过一个建议,我尝试了这个:
.Success(let data):
let data_ar = data as! JSON // or NSDictionary or NSString
self.updateUISuccess(data_ar)
case .Failure(let data, let error):
print("Request failed with error: \(error)")
}
但应用程序在 let data_ar = data as! JSON // or NSDictionary or NSString 处崩溃
【问题讨论】:
-
原
json哪里来的? -
你能从 Alamofire 中删除错误吗?
-
我不太了解@LGL。如果您查看代码的底部块(更新到 xcode7 后我所做的),我没有错误作为参数
标签: ios json swift2 swifty-json