Returning an API in Swift

Asked

Viewed 33 times

0

Hello, yesterday I was doing the requisicao as in the code below, and was returning normally. But today when running again (without any change) the code is not returning me, it comes up to this line:

let objetoFilme = try JSONDecoder().decode(ModeloFilme.self, from: data)

and join the catch

import Foundation
import Alamofire

protocol filmesProtocolo: class {
    func recuperaFilmes()
}

protocol RespostaAPI {
    func success(Modelo: ModeloFilme)
    func failure()
}

class FilmeAPI: NSObject, filmesProtocolo {
    
    var delegate: RespostaAPI?

    func configura(delegate: RespostaAPI){
        self.delegate = delegate
    }
    
    // MARK: - GET

    func recuperaFilmes()  {
        Alamofire.request("url", method: .get).responseJSON { (response) in

            switch response.result{
                
            case .success:
                
                if let resposta = response.result.value as? Dictionary<String, Any> {
                    do {
                        guard let data = response.data else {return}
                        let objetoFilme = try JSONDecoder().decode(ModeloFilme.self, from: data)
                        self.delegate?.success(Modelo: objetoFilme)
                    } catch {
                        print("Falhou aqui")
                    }
                  
                    }
                break
            case .failure:
                print(response.error!)
                
                break
                }
            }
        }

}

  • 1

    Exchange the print("Falhou aqui") for print(error), perhaps the value of data have new keys, print it tbm.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.