首页 > 解决方案 > 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"
}

提前谢谢了!亚历克斯

标签: iosswiftalamofireswifty-json

解决方案


你需要

 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并且longitudedouble值不是Any


推荐阅读