Skip to content

Instantly share code, notes, and snippets.

@alexpyoung
Last active October 19, 2016 04:42
Show Gist options
  • Save alexpyoung/e94e29e5df4a20edbc78390031e1d5a9 to your computer and use it in GitHub Desktop.
Save alexpyoung/e94e29e5df4a20edbc78390031e1d5a9 to your computer and use it in GitHub Desktop.
final class JSONLoader {
static func path(forResource name: String) throws -> String {
let bundle = NSBundle(forClass: JSONLoader.self)
guard let path = bundle.pathForResource(name, ofType: "json") else {
// TODO
throw
}
return path
}
static func dictFromResource(withName name: String) throws -> [String:AnyObject] {
let path = try self.path(forResource: name)
let data = NSData.dataWithContentsOfMappedFile(path)
guard let object = NSJSONSerialization.JSONObjectWithData(data, options: .mutableContainers),
let dict = object as? [String:AnyObject]
else {
// TODO
throw
}
return dict
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment