Created
January 29, 2019 11:15
-
-
Save charlieInDen/4c37f1dca3b8e0f6cbc6b8acdb7440e3 to your computer and use it in GitHub Desktop.
Sample code for making request to server with query parameter and then parsing data and updating a view
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
import CoreLocation | |
struct PointOfInterest: Codable, Equatable { | |
var name: String | |
} | |
class PointOfInterestSample { | |
var tableView: UITableView = UITableView() | |
var tableValues:[PointOfInterest]? | |
func handleError(_ error: Error?) -> Void { | |
print(error!) | |
} | |
func loadData(near coord: CLLocationCoordinate2D) { | |
let url = URL(string: "/locations?lat=\(coord.latitude)&long=\(coord.longitude)")! | |
URLSession.shared.dataTask(with: url) { data, response, error in | |
guard let data = data else { self.handleError(error); return } | |
do { | |
let values = try JSONDecoder().decode([PointOfInterest].self, from: data) | |
DispatchQueue.main.async { | |
self.tableValues = values | |
self.tableView.reloadData() | |
} | |
} catch { | |
self.handleError(error) | |
} | |
}.resume() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment