Skip to content

Instantly share code, notes, and snippets.

@charlieInDen
Created January 29, 2019 11:15
Show Gist options
  • Save charlieInDen/4c37f1dca3b8e0f6cbc6b8acdb7440e3 to your computer and use it in GitHub Desktop.
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
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