ios - Alamofire Dark Sky API 请求 Swift

标签 ios swift alamofire swifty-json

我正在尝试完成对黑暗天空 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"
    }

}

而我的“Did 更新方法”如下使用手机 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 中,同时 latitudelongitudedouble 值而不是 Any

关于ios - Alamofire Dark Sky API 请求 Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54189628/

相关文章:

ios - 子类 UIView 不可见,但内容可见

ios - 为 ios 8 自定义键盘扩展获取 "Terminated due to Memory error"

ios - swift 中的 gridLayout 问题

ios - 如何将圆角半径添加到 UIView 周围的虚线边框

ios - swift 中的雅虎天气 API oauth 请求中的额外参数

ios - 如何检索 UITableViewCell 的缩略图

ios - TableView 从 : numberOfRowsInSection 中的最后一部分开始

ios - 使用 alamofire upload 将图像上传到 AWS S3

ios - 需要 SwiftyJSON 模型和带有嵌套 for 循环的 api 调用,用于带有字典数组的数组

ios - Swift 3.0,Alamofire 4.0 调用中的额外参数 'method'