在Swift 4中无法做Alamofire请求

时间:2017-10-08 14:38:04

标签: swift alamofire

我收到此错误,我不知道为什么会得到它。它没有显示在图片中,但我确实导入了UIKitAlamofire。如果你能让我知道问题是什么,那就太好了。

我认为主要问题出在Alamofire.request(.GET, currentWeatherURL)部分。

func downloadWeatherDetails(completed: DownloadComplete) {
    //Alamofire Download
    let currentWeatherURL = URL(string: CURRENT_WEATHER_URL)!
    Alamofire.request(.GET, currentWeatherURL).responseJSON { response in
        let result = response.result
        print(result)
    }
}

image

1 个答案:

答案 0 :(得分:3)

您的代码存在以下问题:

  1. 您的参数顺序错误,请求网址应 prepend 方法类型,其名称应为method
  2. Alamofire
  3. 中的方法类型现在小写
  4. 不应该强行打开 URL初始值设定项 - 此外,Alamofire为您执行此操作自动,只需传递字符串作为参数
  5. 以下是您修改的代码:

    func downloadWeatherDetails(completed: DownloadComplete) {
        Alamofire.request(CURRENT_WEATHER_URL, method: .get).responseJSON { response in
            let result = response.result
            print(result)
        }
    }
    
相关问题