Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let url = URL(string: "https://httpbin.org/anything")!
- let task = session.dataTask(with: url) { data, response, error in
- // ensure there is no error for this HTTP response
- guard error == nil else {
- print ("error: \(error!)")
- return
- }
- // ensure there is data returned from this HTTP response
- guard let content = data else {
- print("No data")
- return
- }
- // serialise the data / NSData object into Dictionary [String : Any]
- guard let json = (try? JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers)) as? [String: Any] else {
- print("Not containing JSON")
- return
- }
- print("gotten json response dictionary is \n \(json)")
- // update UI using the response here
- }
- // execute the HTTP request
- task.resume()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement