Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- struct dataJSON: Decodable {
- var userId: Int?
- var id: Int?
- var title: String?
- var body: String?
- }
- var userId: Int = -2
- print(userId)
- let requestURL = URL(string: "http://jsonplaceholder.typicode.com/posts/1")
- URLSession.shared.dataTask(with: requestURL!) { (data, response, error) in
- guard let data = data else { return }
- guard error == nil else { return }
- do { let loadedData = try JSONDecoder().decode(dataJSON.self, from: data)
- userId = loadedData.id!
- } catch let error {print(error)}
- print("Before resume = \(userId)")
- }.resume()
- sleep(4)
- print("Result \(userId)")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement