Skip to content

Instantly share code, notes, and snippets.

@kevindelord
Last active August 29, 2015 14:21
Show Gist options
  • Save kevindelord/be2801426eb3ad862437 to your computer and use it in GitHub Desktop.
Save kevindelord/be2801426eb3ad862437 to your computer and use it in GitHub Desktop.
// This very useful code parses a `.json` file using the library SwiftyJSON.
// Add this in a class, maybe in your Data Manager:
class func addDefaultEntities() {
if let file = NSBundle(forClass:AppDelegate.self).pathForResource(JSON.Constants.FileName, ofType: JSON.Constants.Type) {
if let data = NSData(contentsOfFile: file) {
for (index: String, bookJSON: JSON) in JSON(data:data) {
let dict = bookJSON.dictionaryObject
// do something with the dictionary
}
}
}
}
// And this in your extensions file:
extension JSON {
struct Constants {
static let FileName = "your_data_local_filename"
static let Type = "json"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment