Skip to content

Instantly share code, notes, and snippets.

Created August 22, 2014 18:29
  • Star 6 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Swift API call & parsing JSON
import Foundation
import XCPlayground
* Paste all the code from the following file
let urlPath = ""
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
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 {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment