Skip to content

Instantly share code, notes, and snippets.

@thanhluu
Created May 26, 2016 15:50
Show Gist options
  • Save thanhluu/080375c9dcd3fa76646dbaae8d16366e to your computer and use it in GitHub Desktop.
Save thanhluu/080375c9dcd3fa76646dbaae8d16366e to your computer and use it in GitHub Desktop.
Closures in iOS
import Foundation
let label = UILabel()
func getRecentBlogPost(completion: NSURLResponse -> Void) {
let session = NSURLSession(configuration: .defaultSessionConfiguration())
let url = NSURL(string: "http://blog.teamtreehouse.com/api/get_recent_summary?count=20")!
let request = NSURLRequest(URL: url)
let dataTask = session.dataTaskWithRequest(request) { data, response, error in
// Execute body of closure
completion(response!)
}
dataTask.resume()
}
getRecentBlogPost() { response in
print(response)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment