Skip to content

Instantly share code, notes, and snippets.

@mbogh
Created August 29, 2015 08:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbogh/63b3bdc172ef3ce092ac to your computer and use it in GitHub Desktop.
Save mbogh/63b3bdc172ef3ce092ac to your computer and use it in GitHub Desktop.
extension JSONObject {
/// Returns a JSON object initialized by reading into it the data from the file specified by a given path.
/// - Parameter path: The absolute path of the file from which to read data.
/// - Returns: A JSON object initialized by reading into it the data from the file specified by path.
convenience init?(contentsOfFile path: String) {
guard let data = NSData(contentsOfFile: path),
json = (try? NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments)) as? [String : AnyObject] else { return nil }
self.init(dictionary: json)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment