Skip to content

Instantly share code, notes, and snippets.

@Exey
Last active September 1, 2021 17:14
Show Gist options
  • Save Exey/1b677f7e0b2f50b6f5dfb121d9ce2302 to your computer and use it in GitHub Desktop.
Save Exey/1b677f7e0b2f50b6f5dfb121d9ce2302 to your computer and use it in GitHub Desktop.
Mock.swift
struct Mock {}
extension Mock {
static func decode<T>(forResource: String) -> T? where T: Decodable {
var result: T? = nil
do {
if let path = Bundle.main.path(forResource: forResource, ofType: "json"), let jsonData = try String(contentsOfFile: path).data(using: .utf8) {
result = try JSONDecoder().decode(T.self, from: jsonData)
}
} catch {
assertionFailure("decode \(forResource) \n \(error)")
}
return result
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment