Skip to content

Instantly share code, notes, and snippets.

@Kmohamed
Created March 2, 2019 14:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kmohamed/db4ac18c237cba6857328cf6d4981df0 to your computer and use it in GitHub Desktop.
Save Kmohamed/db4ac18c237cba6857328cf6d4981df0 to your computer and use it in GitHub Desktop.
func fetchMovies() {
self.networkLayer.executeGETRequest(api: "/Movies", completionBlock: { (data) in
if let moviesData = data {
let movies = self.parseMovies(data: moviesData)
if let delegate = self.delegate {
delegate.didFetchMovies(success: true, movies: movies)
return
}
}
if let delegate = self.delegate {
delegate.didFetchMovies(success: false, movies: [])
return
}
})
}
func parseMovies(data:Data) -> [Movie] {
do {
var movies:[Movie] = []
if let jsonArray = try JSONSerialization.jsonObject(with: data, options : .allowFragments) as? [[String:String]] {
for object in jsonArray {
let movie = Movie()
movie.name = object["name"]
movie.rating = object["ratting"]
movies.append(movie)
}
return movies
} else {
return []
}
} catch let error as NSError {
print(error)
}
return []
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment