Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ggthedev/c14ce4732429af71c9134b557ab64ca2 to your computer and use it in GitHub Desktop.
Save ggthedev/c14ce4732429af71c9134b557ab64ca2 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")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment