Skip to content

Instantly share code, notes, and snippets.

@jeffdonthemic
Created August 22, 2014 18:29
Show Gist options
  • Save jeffdonthemic/d77e6626606ab5fccfd3 to your computer and use it in GitHub Desktop.
Save jeffdonthemic/d77e6626606ab5fccfd3 to your computer and use it in GitHub Desktop.
Swift API call & parsing JSON
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