Last active
October 5, 2015 01:52
-
-
Save cemolcay/cb0a8b485dc1a15b2ed8 to your computer and use it in GitHub Desktop.
swift json request
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
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