Created
March 2, 2019 14:55
-
-
Save Kmohamed/db4ac18c237cba6857328cf6d4981df0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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