0
How do I get access to an attribute of one class in another? It may sound silly, but I’m learning now Swift and still getting confused with simple things.
I’m trying to make a movie app using an API. I’m also using Alamofire.
However, this problem when trying to access the id of a movie. In the class I request for api I need to put the movie id together with the url (api prompts you to do so). And I’m having a hard time.
I created a struct that can take all the data that the api returns.
struct DetalhesFilme {
let id: Int
let titulo: String
let nota: Float
let ano: String
let descricao: String
let posterPath: String
let background: String
init(dados: [String: Any]) {
let media = dados["vote_average"] as! NSNumber
nota = media.floatValue
titulo = dados["title"] as! String
ano = dados["release_date"] as! String
descricao = dados["overview"] as! String
posterPath = dados["poster_path"] as! String
background = dados["backdrop_path"] as! String
id = dados["id"] as! Int
}
And I made another class that requires it in the API. This is where it is wrong, in Let id = idFilme?. id
Is appearing "Constant 'idFilme' used before being initialized".
class DetalhesAPI {
class func requisicao(completation: @escaping ([DetalhesFilme]) -> Void) {
let idFilme: DetalhesFilme?
let id = idFilme?.id //Constant 'idFilme' used before being initialized
let filmeDetalhes: [DetalhesFilme] = []
Alamofire.request("https://api.themoviedb.org/3/movie/\(id)?api_key=4b7fdfc0addcfa48e0168d4bcd77206f", method: .get).responseJSON { (response) in
switch response.result {
case .success:
if let resposta = response.result.value as? Dictionary<String, Any> {
guard let detalhesLista = resposta as? [[String: Any]] else {
return
}
completation(filmeDetalhes)
}
case .failure:
print(response.error!)
}
}
}