Guest User

Untitled

a guest
Jun 18th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. class PlaceDataFetcher: PlaceDataFetcherProtocol {
  2.  
  3. func fetchPlaces(completion: ([Place]?,_ errorMessage: String?)->()) {
  4. guard let path = Bundle.main.path(forResource: "PlacesList", ofType: "json") else {
  5. completion(nil, "There is a problem in fetching places for you.")
  6. return
  7. }
  8. do {
  9. let data = try Data(contentsOf: URL(fileURLWithPath: path), options: .mappedIfSafe)
  10. let json = try JSONSerialization.jsonObject(with: data, options: .mutableLeaves)
  11. guard let jsonResult = json as? [String: Any] else {
  12. completion(nil, "There is a problem in fetching places for you.")
  13. return
  14. }
  15. guard let results = jsonResult["results"] as? [[String: Any]] else { return }
  16. completion(self.placesListFrom(results: results), nil)
  17.  
  18. } catch {
  19. completion(nil, "There is a problem in fetching places for you.")
  20. }
  21. }
  22.  
  23. private func placesListFrom(results: [[String: Any]]) -> [Place] {
  24. var places = [Place]()
  25. for place in results {
  26. places.append(Place(attributes: place))
  27. }
  28. return places
  29. }
  30.  
  31. }
Add Comment
Please, Sign In to add comment