Created
May 13, 2020 13:04
-
-
Save BlaShadow/6ba86df3656e7b0150020489da8e0054 to your computer and use it in GitHub Desktop.
Swift HTTP Call
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
httpCall(url: 'http://worldtimeapi.org/api/timezone/Europe/Rome') { (result) in | |
print(result) | |
} | |
func httpCall(url: String, completion: @escaping (_: [String: Any]) -> Void) { | |
let url = URL(string: url)! | |
let task = URLSession.shared.dataTask(with: url) {(data, response, error) in | |
guard let data = data else { return } | |
do { | |
let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] | |
completion(json ?? [:]) | |
} catch { | |
print("JSON error: \(error.localizedDescription)") | |
completion([:]) | |
} | |
} | |
task.resume() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment