Advertisement
ptsytovich

Untitled

Nov 8th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 0.87 KB | None | 0 0
  1. let url = URL(string: "https://httpbin.org/anything")!
  2. let task = session.dataTask(with: url) { data, response, error in
  3.    
  4.     // ensure there is no error for this HTTP response
  5.     guard error == nil else {
  6.         print ("error: \(error!)")
  7.         return
  8.     }
  9.    
  10.     // ensure there is data returned from this HTTP response
  11.     guard let content = data else {
  12.         print("No data")
  13.         return
  14.     }
  15.    
  16.     // serialise the data / NSData object into Dictionary [String : Any]
  17.     guard let json = (try? JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers)) as? [String: Any] else {
  18.         print("Not containing JSON")
  19.         return
  20.     }
  21.    
  22.     print("gotten json response dictionary is \n \(json)")
  23.     // update UI using the response here
  24. }
  25.  
  26. // execute the HTTP request
  27. task.resume()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement