Skip to content

Instantly share code, notes, and snippets.

@harley
Last active June 19, 2017 06:51
Show Gist options
  • Save harley/7d5ea01a5122bc65c43f to your computer and use it in GitHub Desktop.
Save harley/7d5ea01a5122bc65c43f to your computer and use it in GitHub Desktop.
// sample code in the Swift class I'm teaching, on making network request to Instagram to get photos
var clientId = "Put your client id here"
// if you like to use Swift's Dictionary data type, you can use: `let photos = [Dictionary<String, AnyObject>]()`
let photos = [NSDictionary]()
var url = NSURL(string: "https://api.instagram.com/v1/media/popular?client_id=\(clientId)")!
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithURL(url) { (data: NSData?, response: NSURLResponse?, error: NSError?) -> Void in
guard error == nil else {
print("error loading from URL", error!)
return
}
let json = try! NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments) as! NSDictionary
self.photos = json["data"] as! [NSDictionary]
print("photos", self.photos)
}
task.resume()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment