Created
April 12, 2019 14:20
-
-
Save madeindjs/fbb52cb422464e068c9806951d65118c to your computer and use it in GitHub Desktop.
Fetch & Parse JSOn from raspberry-cook.fr
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 Foundation | |
let decoder = JSONDecoder() | |
let uri : String = "http://raspberry-cook.fr/recipes/buddha-bowl-au-thon-cuit.json" | |
let feedUrl = URL(string: uri)! | |
struct Recipe : Codable { | |
var id : Int | |
var name : String | |
} | |
func parseResponse(_ jsonData : Data) -> Recipe? { | |
do { | |
let recipe = try decoder.decode(Recipe.self, from: jsonData) | |
return recipe | |
} catch { | |
return nil | |
} | |
} | |
var isFinished = false | |
let task = URLSession.shared.dataTask(with: feedUrl) { (data, response, error) in | |
guard let data = data else { | |
print("Error during fetching query: \(error?.localizedDescription)") | |
isFinished = true | |
return | |
} | |
let recipe = parseResponse(data) | |
print(recipe?.name) | |
isFinished = true | |
} | |
task.resume() | |
while(!isFinished) { | |
sleep(1) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment