2

I'm making a application in Swift that makes use of a API from the TMDB. I found a wrapper for this. Installed it with Cocoapods and now I'm wrestling with the functions. I think the readme file is outdated. So with this function I want to retrieve a list of genres.

For example a function in the framework:

    ///Get the list of tv or movie genres.
  open class func genres(_ api_key: String!, listType: GenresListType, language: String?, completion: @escaping (_ clientReturn: ClientReturn, _ data: [GenresMDB]?) -> ()) -> (){
    Client.Genres(api_key, listType: listType.rawValue, language: language, genreId: 0, page: nil, include_all_movies: nil, include_adult: nil, movieList: false){
      apiReturn in
      if(apiReturn.error == nil){
        completion(apiReturn, GenresMDB.initialize(json: apiReturn.json!["genres"]))
      }else{
        completion(apiReturn, nil)
      }
    }
  }

In the Readme the function is called like this(get the list of movie OR tv genre):

GenresMDB.genres(apikey, listType: .tv, language: "en"){
  apiReturn, genres in
  if let genres = genres{
    genres.forEach{
      print($0.name)
    }
  }
}

But I think its outdated.

So now I call the function in a viewController.swift like this:

    GenresMDB.genres(apiKey, listType: .movie, language: "en", completion: (ClientReturn, [GenresMDB]?) -> ()) {
}

I want a list of genres for movies with language english. But I I stil don't understand what the completion handler will do. Or how I save the data returned by the function(I think in JSON format). Is there someone who understands this function and an explain it to me?

Coen Walter
  • 79
  • 1
  • 1
  • 4

1 Answers1

0

nothing much has changed since the wiki was last updated. Below is an example of how to achieve what you want.

GenresMDB.genres(key, listType: .movie, language: "en", completion: {
    error, genresData in
    //Do something with genres returned
    print(genresData[0].name)
})

Also you can check out the documentation on using closures / completion handlers in swift

https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html

kye
  • 2,166
  • 3
  • 27
  • 41