Skip to content

Instantly share code, notes, and snippets.

@charleshkang
Created June 15, 2016 16:52
Show Gist options
  • Save charleshkang/b508d4e58271e3c7e529fcdd673cacda to your computer and use it in GitHub Desktop.
Save charleshkang/b508d4e58271e3c7e529fcdd673cacda to your computer and use it in GitHub Desktop.
import Foundation
import SwiftyJSON
class BadgeAPI {
public func main(function: Void -> Void) {
dispatch_async(dispatch_get_main_queue(), function)
let jsonURL = "http://www.khanacademy.org/api/v1/badges"
if let url = NSURL(string: jsonURL) {
if let data = try? NSData(contentsOfURL: url, options: []) {
let json = JSON(data: data)
fetchBadgeJSON(json)
}
}
}
public func background(function: Void -> Void) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), function)
}
public func fetchBadgeJSON(json: JSON)
{
var objects = [[String: String]]()
for (_, result) in json {
let title = result["description"].stringValue
let smallIcon = result["icons"]["small"].stringValue
let category = result["badge_category"].stringValue
let points = result["points"].stringValue
let obj = ["title": title, "icon": smallIcon, "category": category, "points": points]
objects.append(obj)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment