Skip to content

Instantly share code, notes, and snippets.

@madeindjs
Created April 12, 2019 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save madeindjs/fbb52cb422464e068c9806951d65118c to your computer and use it in GitHub Desktop.
Save madeindjs/fbb52cb422464e068c9806951d65118c to your computer and use it in GitHub Desktop.
Fetch & Parse JSOn from raspberry-cook.fr
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