Last active
April 10, 2019 00:24
-
-
Save kristopherjohnson/484130772001a475d584 to your computer and use it in GitHub Desktop.
Swift Playground using NSURLSession
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | |
} |
Need to call XCPlaygroundPage.currentPage.finishExecution()
when ready and execution has finished.
thank you @danielgalasko .
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
deprecated, should use
XCPlaygroundPage.currentPage.needsIndefiniteExecution = true