Created
November 26, 2018 12:30
-
-
Save serhatsezer/629a1817697845e4dadedc77677c4271 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
struct User: Codable { | |
let userId: Int | |
let id: Int | |
let title: String | |
let completed: Bool | |
} | |
private func makeRequest() { | |
let url = URL(string: "https://jsonplaceholder.typicode.com/todos/1")! | |
let urlRequest = URLRequest(url: url, cachePolicy: .returnCacheDataElseLoad, timeoutInterval: 1.0) | |
let urlSession = URLSession(configuration: .default) | |
let dataTask: URLSessionDataTask | |
dataTask = urlSession.dataTask(with: urlRequest) { (data, urlResponse, error) in | |
if let error = error { | |
print(error.localizedDescription) | |
} else if let data = data { | |
let jsonDecoder = JSONDecoder() | |
let user = try! jsonDecoder.decode(User.self, from: data) | |
print(user.title) | |
} | |
} | |
dataTask.resume() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment