Skip to content

Instantly share code, notes, and snippets.

@mbogh
Created August 29, 2015 08:25
Embed
What would you like to do?
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