Skip to content

Instantly share code, notes, and snippets.

@sag333ar
Last active October 10, 2015 13:01
Show Gist options
  • Save sag333ar/829c644eb78a647b287b to your computer and use it in GitHub Desktop.
Save sag333ar/829c644eb78a647b287b to your computer and use it in GitHub Desktop.
Load Data from file & parse JSON in Swift 2
// Swift 2
// try will throw an error if there any &
// catch will directly catch with 'error'
let str:String? = NSBundle.mainBundle().pathForResource("IndiaPost", ofType: "json")
let data:NSData? = NSData(contentsOfFile: str!)
do {
let jsonData:AnyObject? = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments)
if let arrayData:NSArray = jsonData as? NSArray {
self.arrayOfLeftMenuItems = arrayData
}
} catch {
print(error)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment