Created
September 22, 2014 15:32
-
-
Save scttymn/e021578857633372752e to your computer and use it in GitHub Desktop.
Simple JSON Parse in Swift
This file contains hidden or 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
import UIKit | |
class ViewController: UIViewController { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view, typically from a nib. | |
let urlPath = "http://www.telize.com/geoip" | |
let url = NSURL(string: urlPath) | |
let session = NSURLSession.sharedSession() | |
let task: Void = session.dataTaskWithURL(url, completionHandler: {data, response, error -> Void in | |
if (error != nil) { | |
println(error) | |
} else { | |
// Normally you would handle errors. Setting to nil for this example. | |
let jsonResult = NSJSONSerialization.JSONObjectWithData( | |
data, | |
options: NSJSONReadingOptions.MutableContainers, | |
error: nil) as NSDictionary | |
println(jsonResult) | |
println(jsonResult["country_code"]) | |
} | |
}).resume() | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment