ios - Alamofire Dark Sky API 请求 Swift
问题描述
我正在尝试完成对黑暗天空 API 的 .get 请求,该请求需要以下参数来完成请求: https://api.darksky.net/forecast/[key]/[latitude],[longitude]
我的代码如下所示,我在“方法”调用中得到一个“额外参数”,我认为这是因为我的函数数据类型错误。我一直在尝试四处寻找正确的,但我无法做到正确。
我的 Alamofire 请求如下:
//Get wind data method
func getWindData(url: String, key: Any, latitude: Any, longitude: Any) {
Alamofire.request(url, method: .get, key, latitude, longitude).responseJSON {
response in
if response.result.isSuccess {
print("Success! Got the weather data")
let windJSON : JSON = JSON(response.result.value!)
print(windJSON)
}
else {
print("Error \(String(describing: response.result.error))") }
self.yard.text = "Connection issues"
}
}
我的“更新方法”如下使用手机gps获取用户所需的经纬度:
//Did update method
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations[locations.count - 1]
if location.horizontalAccuracy > 0 {
self.locationManager.stopUpdatingLocation()
print("latitude = \(location.coordinate.latitude), longitude = \(location.coordinate.longitude)")
let latitude = String(location.coordinate.latitude)
let longitude = String(location.coordinate.longitude)
getWindData(url: base_URL, key: api_Key, latitude: latitude, longitude: longitude)
}
}
//Did fail with error method
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print(error)
yard.text = "Error"
}
提前谢谢了!亚历克斯
解决方案
你需要
let openWeatherMapBaseURL = "https://api.darksky.net/forecast/Key_Here/"
let urlStr = "\(openWeatherMapBaseURL)\(latitude),\(longitude)"
Alamofire.request(urlStr, method: .get, parameters:nil, encoding: JSONEncoding.default).responseJSON { [weak self] response in
}
不存在
Alamofire.request(url, method: .get, key, latitude, longitude).responseJSON
get 的参数需要在 url 中,latitude
并且longitude
是double
值不是Any
推荐阅读
- python-3.x - 如何使用波形库从波形文件中获取字节数据类型的整数值
- c# - 在脚本中引用/使用预制件的最优化方式
- python - 确保 Dash 应用中 Div 组件的顺序
- php - 从 JSON 数组获取电子邮件
- mysql - 当我使用多个 JOIN 语句时,SUM 计算不正确。其他项目计算正确
- haskell - 在 Haskell 中使用整数列表检索元组列表
- css - CSS 集中元素
- java - Unity Android 方向变化动画显示最后的方向快照
- c# - 如何从 JsonNet 序列化 DateTimeOffset?
- ruby-on-rails - 如何防止工作每小时发送邮件超过一次?