Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //We have some class, HTTPManager, that allows us to perform a GET request to a URL.
- //Upon successful fetch of the data, the success closure is executed with the data (represented as NSData,
- //more or less the raw bytes) and the calling function can then do what it needs with the data.
- //the method signature of HTTPManager.get() is class func get(url: String, success: (data: NSData) -> Void, failure: (error: //String) -> Void) { }
- HTTPManager.get("www.example.com", success: { (data) in
- //we wrap this in a do/catch block (essentially a try/except block) because .JSONObjectWithData() can throw an Error.
- do {
- //we declare it [String: AnyObject] because the types of the values in the JSON object cannot be automatically inferred.
- let json: [String: AnyObject]? = try NSJSONSerialization.JSONObjectWithData(data: data, options: NSJSONReadingOptions.MutableContainers) as? [String: AnyObject]
- <if the json is not nil, do whatever you need with it>
- } catch {
- }
- }, failure: { (error) in
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement