Loading Data from a URL

To load data into a table, you could grab the data from a URL. The following is a small class that you could use to load the JSON data. Create a new file, name it Get.swift, and enter the following in it:

import Foundationclass Get {    class func JSON(url:String,callback:(NSDictionary)->()) {        requestJSON(url,callback)    }    class func requestJSON(url:String,callback:(NSDictionary)->()) {        var nsURL = NSURL(string: url)        let task = NSURLSession.sharedSession().dataTaskWithURL(nsURL) {            (data,response,error) in            var error:NSError?            var response = NSJSONSerialization.JSONObjectWithData(data,               options: NSJSONReadingOptions.MutableContainers, ...

Get Learning Swift™ Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.