Last active
September 1, 2021 17:14
-
-
Save Exey/1b677f7e0b2f50b6f5dfb121d9ce2302 to your computer and use it in GitHub Desktop.
Mock.swift
This file contains 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
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