Last active
June 19, 2017 06:51
-
-
Save harley/7d5ea01a5122bc65c43f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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