Created
March 23, 2018 09:25
-
-
Save ianpartridge/0284961f5699e5fc5d9353ccc4d68a84 to your computer and use it in GitHub Desktop.
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 | |
import PlaygroundSupport | |
struct Film: Decodable | |
{ | |
let title: String | |
} | |
struct Films: Decodable | |
{ | |
let count: Int | |
let results: [Film] | |
} | |
let session = URLSession(configuration: .default) | |
let filmsURL = URL(string: "https://swapi.co/api/films")! | |
let request = URLRequest(url: filmsURL) | |
let task = session.dataTask(with: request) { data, response, error in | |
guard error == nil, let data = data else { | |
return | |
} | |
let decoder = JSONDecoder() | |
do { | |
let films = try decoder.decode(Films.self, from: data) | |
print(films) | |
} catch let error { | |
print(error) | |
} | |
PlaygroundPage.current.finishExecution() | |
} | |
task.resume() | |
PlaygroundPage.current.needsIndefiniteExecution = true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment