Skip to content

Instantly share code, notes, and snippets.

@cemolcay
Last active October 5, 2015 01:52
Show Gist options
  • Save cemolcay/cb0a8b485dc1a15b2ed8 to your computer and use it in GitHub Desktop.
Save cemolcay/cb0a8b485dc1a15b2ed8 to your computer and use it in GitHub Desktop.
swift json request
func json (url: String, response: (AnyObject, NSError?) -> Void) {
NSURLConnection.sendAsynchronousRequest(NSURLRequest(URL: NSURL(string: url)), queue: NSOperationQueue.currentQueue(), completionHandler:{response, data, error in
var jsonError : NSError?
var json : AnyObject = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments, error: &jsonError)
response (json, jsonError)
})
}
/* usage
json("http://www.someurl.com/some/json", {jsonObj, error in
if (!error) {
NSLog("obj %@", [jsonObj])
} else {
NSLog ("error %@", [error])
}
})
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment