Skip to content

Instantly share code, notes, and snippets.

@yhkaplan
Created January 15, 2019 06:03
Show Gist options
  • Save yhkaplan/4b0c1cf6febcfb183afeb3eef4754865 to your computer and use it in GitHub Desktop.
Save yhkaplan/4b0c1cf6febcfb183afeb3eef4754865 to your computer and use it in GitHub Desktop.
Basic explainer resource for beginners
import Foundation
import PlaygroundSupport
// Model
struct Character: Decodable {
let name: String
}
// NW
let url = URL(string: "https://swapi.co/api/people/1/")!
URLSession.shared.dataTask(with: url) { data, res, error in
guard
let data = data,
let string = String(data: data, encoding: .utf8),
let character = try? JSONDecoder().decode(Character.self, from: data)
else {
let errorMessage = error?.localizedDescription ?? "Unknown error"
print(errorMessage)
return
}
print(res)
// print(string)
// print(character)
}.resume()
// Boring setting for Playgrounds
PlaygroundPage.current.needsIndefiniteExecution = true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment