Created
July 18, 2017 17:11
-
-
Save AntonTheDev/ad109c1de51226de273785e8c38adcf9 to your computer and use it in GitHub Desktop.
Load JSON FileManager Extension
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extension FileManager { | |
| class func loadJSON(from file : String) -> Any? { | |
| do { | |
| if let file = Bundle.main.url(forResource: file, withExtension: "json") { | |
| let data = try Data(contentsOf: file) | |
| let json = try JSONSerialization.jsonObject(with: data, options: []) | |
| if let dict = json as? [String: Any] { | |
| return dict | |
| } else if let array = json as? [Any] { | |
| return array | |
| } else { | |
| print("JSON is invalid") | |
| } | |
| } else { | |
| print("no file") | |
| } | |
| } catch { | |
| print(error.localizedDescription) | |
| } | |
| return nil | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment