Create a gist now

Instantly share code, notes, and snippets.

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