Tony Merritt
Tony Merritt

Reputation: 1197

Argument type NSURL does not conform

I am working from an old tutorial swift2, Alamofire 3, but I am using swift 3, Alamofire 4.

I have changed most things successfully, but I am running into a problem. with this code area.

let url = NSURL(string: _pokemonUrl)!
        Alamofire.request(url).responseJSON { response in
            let result = response.result

I am getting an error that says:

Argument type NSURL does not conform to expected type URLRequestConvertible.

it does give me the option of adding in as! URLRequestConvertible after the (url) but it crashes again after compile and when i press the button to get the info. it gives an error of:

Could not cast value of type 'NSURL' (0x117e99338) to 'Alamofire.URLRequestConvertible' (0x1189ab120).

if I change NSURL to Url then it moves forward in the code but when it gets to the print statement it crashes and gives the error:

fatal error: unexpectedly found nil while unwrapping an Optional value

here is that code below.

let url = URL(string: _pokemonUrl)!
        Alamofire.request(url).responseJSON { response in
            let result = response.result



            if let dict = result.value as? Dictionary<String, AnyObject> {

                if let weight = dict["weight"] as? String {
                    self._weight = weight
                }

                if let height = dict["height"] as? String {
                    self._height = height
                }

                if let baseAttack = dict["attack"] as? Int {
                    self._baseAttack = "\(baseAttack)"
                }



                if let defense = dict["defense"] as? Int {
                    self._defense = "\(defense)"
                }


                print(self._weight)
                print(self._height)
                print(self._baseAttack)
                print(self._defense)

I have tried to change all to Int but i get the same error.

Can anyone shed any light on this for me.

if it helps I put a break point in after print("Here") in the next code and it gives me the following error.

let url = URL(string: _pokemonUrl)!
        Alamofire.request(url).responseJSON { response in
            let result = response.result

            print(result.value.debugDescription)
            print("Here")

error comes up:

Optional({ "error_message" = "Sorry, this request could not be processed. Please try again later."; }) Here

Thanks in advance, Tony

Upvotes: 1

Views: 2448

Answers (2)

Jaime Agudo
Jaime Agudo

Reputation: 8296

You almost did it guys, just a cast to URL was missing.

let nsurl = NSURL(string: _pokemonUrl)!
let request = URLRequest(url: nsurl as URL)
Alamofire.request(request).responseJSON { response in
   let result = response.result
   ...
} 

Upvotes: 2

Narine Balasanyan
Narine Balasanyan

Reputation: 51

I tried folowing & it's helped

func downloadPokemonDetails(completed: DownloadComplete) {
        let url = URL(string: _pokemonUrl)!
        Alamofire.request(URLRequest(url: url)).responseJSON { response in
            if let result = response.result.value as? [String: Any]{
                print(result.debugDescription)
            }
        }
    }

Upvotes: 0

Related Questions