Sirius 8.611
Sirius 8.611

Reputation: 11

Cannot do Alamofire request in Swift 4

I get this error and I have no idea why I get it. It is not shown in the picture but I do import UIKit and Alamofire. It would be great if you could let me know what the problem is.

I think the main problem is in the Alamofire.request(.GET, currentWeatherURL) part.

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

Upvotes: 1

Views: 1798

Answers (1)

glyvox
glyvox

Reputation: 58049

There are several issues with your code:

  1. The order of your parameters is wrong, the request URL should prepend the method type, which should have the name method.
  2. Method types are now lowercased in Alamofire.
  3. You should not force unwrap the URL initializer - moreover, Alamofire does this for you automatically, just pass the string as a parameter.

Here is your code, fixed:

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

Upvotes: 3

Related Questions