Created
February 25, 2019 19:17
-
-
Save chamook/ba052a104197023689095ac29ea9581b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| 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