Skip to content

Instantly share code, notes, and snippets.

@chenr2
Last active August 29, 2015 14:25
Show Gist options
  • Save chenr2/66f15d3296d8f8f60f13 to your computer and use it in GitHub Desktop.
Save chenr2/66f15d3296d8f8f60f13 to your computer and use it in GitHub Desktop.
Swift: Demonstrating the Network Starter Kit for the Columbia iOS meetup

Demo of the Network Starter Kit

See Network Starter Kit.

This is the script for Tuesday's Columbia iOS meetup.

GET Request

Problem statement.

Just to get this output: label:: Agar.io

we have all this code:

let url = NSURL(string: "https://itunes.apple.com/us/rss/topfreeapplications/limit=10/json")
let request = NSMutableURLRequest(URL: url!)
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithRequest(request){
    (data, response, error) -> Void in
    println("data:: \(data)")
    println("response:: \(response)")
    println("error:: \(error)")
    
    // data: is still totally unreadable, and needs to be converted to JSON

    var jsonError: NSError?
    if let json = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: &jsonError) as? [String: AnyObject] {
        
        println("json:: \(json)")
        
        if let feed = json["feed"] as? NSDictionary {
            if let entry = feed["entry"] as? NSArray {
                if let appObject = entry[0] as? NSDictionary {
                    if let imName = appObject["im:name"] as? NSDictionary {
                        if let label = imName["label"] as? String {
                            println("label:: \(label)")
                        }
                    }
                }
            }
        }
    }

}
task.resume()

Pain points:

  • Lots of boilerplate just to setup a network request. Imagine if you have a dozen API endpoints.
  • NSJSONSerialization is kind of yucky. We still need to protect against jsonError bombing.
  • Check out the if-let pyramid of doom

GET using AlamoFire and SwiftyJSON

Syntax is tighter and easier to read.

let url = "https://itunes.apple.com/us/rss/topfreeapplications/limit=10/json"
request(.GET, url)
    .responseJSON { (_, _, data, _) in
        let json = JSON(data!)
        let label = json["feed"]["entry"][0]["im:name"]["label"].stringValue
        println("label:: \(label)")
    }

What about POST?

This is what a vanilla POST looks like. Not only do you need to convert your dictionary to JSON, you also need to set application/json headers.

let url = NSURL(string: "http://httpbin.org/post")
let params = [ "foo" : "bar" ]
var err: NSError?

let request = NSMutableURLRequest(URL: url!)
request.HTTPMethod = "POST"
request.HTTPBody = NSJSONSerialization.dataWithJSONObject(params, options: nil, error: &err)
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")

let session = NSURLSession.sharedSession()
let task = session.dataTaskWithRequest(request) { data, response, error -> Void in
    println("data:: \(data)")
    println("response:: \(response)")
    println("error:: \(error)")
    
    var jsonError: NSError?
    if let json = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: &jsonError) as? [String: AnyObject] {
        
        println("json:: \(json)")
    }
    
}
task.resume()

A better way to POST

This does the same thing.

let params = [ "foo" : "bar" ]
request(.POST, "http://httpbin.org/post", parameters: params, encoding: .JSON)
    .responseJSON { (_, _, data, _) in
        println("data:: \(data)")
    }

How about downloading images

This is the normal way:

let url = NSURL(string: "http://tsumtsumdisney.com/wp-content/uploads/2014/10/Snow-White-and-the-Seven-Dwarfs-Tsum-Tsum-Set-of-8-0-0.jpg")
let request = NSURLRequest(URL: url!)
let mainQueue = NSOperationQueue.mainQueue()
NSURLConnection.sendAsynchronousRequest(request, queue: mainQueue) {
    (response, data, error) -> Void in
    if error == nil {
        let image = UIImage(data: data)
        dispatch_async(dispatch_get_main_queue()) {
            self.imageView.image = image
        }
    }
}

A better way to download images

request(.GET, "http://tsumtsumdisney.com/wp-content/uploads/2014/10/Snow-White-and-the-Seven-Dwarfs-Tsum-Tsum-Set-of-8-0-0.jpg")
    .responseImage() { (request, _, image, error) in
        if error == nil && image != nil {
            self.imageView.image = image
        }
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment