Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save AntonTheDev/ad109c1de51226de273785e8c38adcf9 to your computer and use it in GitHub Desktop.

Select an option

Save AntonTheDev/ad109c1de51226de273785e8c38adcf9 to your computer and use it in GitHub Desktop.
Load JSON FileManager Extension
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