Skip to content

Instantly share code, notes, and snippets.

@pouriaalmassi
Created February 18, 2015 02:01
Show Gist options
  • Save pouriaalmassi/31de187cc5fc67eeb570 to your computer and use it in GitHub Desktop.
Save pouriaalmassi/31de187cc5fc67eeb570 to your computer and use it in GitHub Desktop.
NSURLSession in a Playground
// Playground - noun: a place where people can play
import UIKit
import XCPlayground
XCPSetExecutionShouldContinueIndefinitely(continueIndefinitely: true)
let path = "http://www.reddit.com/r/simpleios.json"
let session = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration())
let url = NSURL(string: path)
let request = NSURLRequest(URL: url!)
let dataTask = session.dataTaskWithRequest(request, completionHandler: { (data, resp, error) in
var error: NSError?
if (data == nil) {
println("Error: \(error?.localizedDescription)")
return;
}
let result = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &error) as? NSDictionary
if let r = result {
println("result: \(r)")
}
})
dataTask.resume()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment