Skip to content

Instantly share code, notes, and snippets.

@chamook
Created February 25, 2019 19:17
Show Gist options
  • Select an option

  • Save chamook/ba052a104197023689095ac29ea9581b to your computer and use it in GitHub Desktop.

Select an option

Save chamook/ba052a104197023689095ac29ea9581b to your computer and use it in GitHub Desktop.
struct Colour: Decodable {
var id: String
var name: String
var hex: String
}
var colours: [Colour] = []
private func loadColours() {
let url = URL(string: "http://localhost:5000/my-colours")
URLSession.shared.dataTask(with: url!) { [weak self] (data, response, error) in
guard error == nil else {
print("Error getting colours")
return
}
guard let content = data else {
print("Couldn't get any colours")
return
}
do {
struct ColourDto: Decodable {
var colours: [Colour]
}
let colourData = try JSONDecoder().decode(ColourDto.self, from: content)
self?.colours = colourData.colours
DispatchQueue.main.async {
self?.tableView?.reloadData()
}
} catch let err {
print("Error decoding json", err)
}
}.resume()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment