Skip to content

Instantly share code, notes, and snippets.

@gavi
Last active December 27, 2015 01:16
Show Gist options
  • Save gavi/4c33433e2b7f68d96142 to your computer and use it in GitHub Desktop.
Save gavi/4c33433e2b7f68d96142 to your computer and use it in GitHub Desktop.
Running asynchronous code in Xcode Playground
import Foundation
import XCPlayground
// Updated for Swift 2.0
let url:NSURL!=NSURL(string: "http://objectgraph.com")
NSURLSession.sharedSession().dataTaskWithURL(url) { data, response, error in
print(NSString(data: data!, encoding: NSUTF8StringEncoding))
print(error)
print(response)
}.resume()
print("This line is printed first as the network call is not complete")
//This will make sure your playground runs indefinitely
XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment