Last active
September 21, 2015 22:39
-
-
Save tcuongtran/78b35aae6822631497a3 to your computer and use it in GitHub Desktop.
Swift 2.0 Perform http request and parse returned data
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() | |
let url = NSURL(string: "http://www.reddit.com/.json") | |
let session = NSURLSession.sharedSession() | |
let task = session.dataTaskWithURL(url!, completionHandler: { | |
(data, repsonse, error) -> Void in | |
if (error != nil) { | |
print(error) | |
} else { | |
do { | |
let jsonResult = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) | |
if let resultObject = jsonResult as? NSDictionary { | |
if let resultObjectData = resultObject["data"] as? NSDictionary { | |
if let resultObjectChildren = resultObjectData["children"] as? NSArray { | |
print(resultObjectChildren[0]) | |
} | |
} | |
} | |
} catch let error as NSError { | |
print(error) | |
} | |
} | |
}) | |
task.resume() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment