Good afternoon,
I'm trying to create a simple function to retrieve some information using an external API with Alamofire in Swift 2.2, but when I want to return the value it's always empty.
I tried to follow some other QA from this website and many more from other websites but I'm not able to return the value from this function.
When I print my variable (overview) inside the Alamofire it's correct, but when I try to return it, it's always empty. What I have to add (or change) in my code to make this work?
Here is my function:
func getOverview(slug: String, clientID: String) -> String {
let movieURL: String = "https://api.trakt.tv/movies/"+slug+"?extended=full"
let headers = ["trakt-api-version":"2", "Content-Type": "application/json", "trakt-api-key": clientID]
var overview: String = ""
Alamofire.request(.GET, movieURL, headers: headers).responseJSON { response in
if response.result.isSuccess {
let movieInfo = JSON(data: response.data!)
overview = self.parseMovieOverview(movieInfo)
// THAT'S CORRECT
print(overview)
} else {
print(response.result.error)
}
}
// THAT'S EMPTY
return overview
}
And that's my call:
// THAT'S ALWAYS EMPTY
overview = getOverview(slug, clientID: clientID)
Edited with the returned value overview:
func parseMovieInfo(json: SwiftyJSON.JSON, clientID: String) -> [Movie] {
for result in json.arrayValue {
let slug = result["ids"]["slug"].stringValue
let title = result["title"].stringValue
let year = result["year"].stringValue
// OVERVIEW
let overview = getOverview(slug, clientID: clientID)
// PICTURE
let picture = getPicture(slug, clientID: clientID)
let movie = Movie(slug: slug, title: title, year: year, overview: overview, picture: picture)
self.movies.append(movie)
}
return movies
}
Much appreciated,
Regards