Created
August 22, 2014 18:29
-
-
Save jeffdonthemic/d77e6626606ab5fccfd3 to your computer and use it in GitHub Desktop.
Swift API call & parsing JSON
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 | |
XCPSetExecutionShouldContinueIndefinitely() | |
/** | |
* Paste all the code from the following file | |
- https://github.com/lingoer/SwiftyJSON/blob/master/SwiftyJSON/SwiftyJSON.swift | |
**/ | |
let urlPath = "http://api.topcoder.com/v2/challenges?pageSize=2" | |
let url: NSURL = NSURL(string: urlPath) | |
let session = NSURLSession.sharedSession() | |
let task = session.dataTaskWithURL(url, completionHandler: {data, response, error -> Void in | |
if error != nil { | |
// If there is an error in the web request, print it to the console | |
println(error.localizedDescription) | |
} | |
var err: NSError? | |
var jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as NSDictionary | |
if err != nil { | |
// If there is an error parsing JSON, print it to the console | |
println("JSON Error \(err!.localizedDescription)") | |
} | |
let json = JSONValue(jsonResult) | |
let count: Int? = json["data"].array?.count | |
println("found \(count!) challenges") | |
if let ct = count { | |
for index in 0...ct-1 { | |
// println(json["data"][index]["challengeName"].string!) | |
if let name = json["data"][index]["challengeName"].string { | |
println(name) | |
} | |
} | |
} | |
}) | |
task.resume() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment