Skip to content

Instantly share code, notes, and snippets.

@ozgurshn
Last active July 8, 2019 09:07
Show Gist options
  • Save ozgurshn/63a59f4d241194bf6a685a3c43f7c8de to your computer and use it in GitHub Desktop.
Save ozgurshn/63a59f4d241194bf6a685a3c43f7c8de to your computer and use it in GitHub Desktop.
Networking Request in Xcode 8.0 Playground.
import UIKit
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
let url = URL(string: "http://ip.jsontest.com/")!
let session = URLSession.shared()
let q = session.dataTask(with: url) { data, response, error in
do {
if let d = data, let dictionary = try JSONSerialization.jsonObject(with: d, options: []) as? [String: AnyObject] {
print(dictionary["ip"]!)
}
}
catch {
print("Error \(error)")
}
}
q.resume()
@tonyatoms
Copy link

tonyatoms commented Apr 11, 2018

This works for me in Xcode 9 Playgrounds by changing

let session = URLSession.shared()
to
let session = URLSession.shared

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment