Skip to content

Instantly share code, notes, and snippets.

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 vkostechko/11b6909e8e0b45d8def0ca779a2d2288 to your computer and use it in GitHub Desktop.
Save vkostechko/11b6909e8e0b45d8def0ca779a2d2288 to your computer and use it in GitHub Desktop.
Swift 3 - Load JSON file
let filePath = Bundle.main.pathForResource("YOUR_FILE_NAME", ofType: "json", inDirectory: nil)
if let filePath = filePath {
do {
let fileUrl = URL(fileURLWithPath: filePath)
let jsonData = try Data(contentsOf: fileUrl, options: .mappedIfSafe)
let json = try? JSONSerialization.jsonObject(with: jsonData)
} catch {
print(error)
fatalError("Unable to read contents of the file url")
}
}
@vkostechko
Copy link
Author

let filePath = Bundle.init(for: type(of: self)).path(forResource: "YOUR_FILE_NAME", ofType: "json")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment