Created
February 18, 2015 02:01
-
-
Save pouriaalmassi/31de187cc5fc67eeb570 to your computer and use it in GitHub Desktop.
NSURLSession in a Playground
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
// 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