Skip to content

Instantly share code, notes, and snippets.

@kristopherjohnson
Last active April 10, 2019 00:24
Show Gist options
  • Save kristopherjohnson/484130772001a475d584 to your computer and use it in GitHub Desktop.
Save kristopherjohnson/484130772001a475d584 to your computer and use it in GitHub Desktop.
Swift Playground using NSURLSession
import Foundation
import XCPlayground
// Let asynchronous code run
XCPSetExecutionShouldContinueIndefinitely()
if let url = NSURL(string: "http://www.google.com/") {
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithURL(url, completionHandler: { (data, response, error) -> Void in
if error != nil {
println("error: \(error.localizedDescription): \(error.userInfo)")
}
else if data != nil {
if let str = NSString(data: data, encoding: NSUTF8StringEncoding) {
println("Received data:\n\(str)")
}
else {
println("unable to convert data to text")
}
}
})
task.resume()
}
else {
println("Unable to create NSURL")
}
@avioli
Copy link

avioli commented Dec 16, 2015

Need to call XCPlaygroundPage.currentPage.finishExecution() when ready and execution has finished.

@kiritmodi2702
Copy link

@ramiresnas
Copy link

thank you @danielgalasko .

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