Created
December 23, 2022 04:12
-
-
Save MickhailP/98f1e9dd5b70a47958cff8b8a425f3c1 to your computer and use it in GitHub Desktop.
GenericBundelDecoder
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
import Foundation | |
extension Bundle { | |
func decode<T: Codable> (_ file: String) -> T { | |
guard let url = self.url(forResource: file, withExtension: nil) else { | |
fatalError("Failed to locate \(file) in bundle.") | |
} | |
guard let data = try? Data(contentsOf: url) else { | |
fatalError("Failed to load \(file) from bundle.") | |
} | |
let decoder = JSONDecoder() | |
// let formatter = DateFormatter() | |
// formatter.dateFormat = "y-MM-dd" | |
// decoder.dateDecodingStrategy = .formatted(formatter) | |
guard let loaded = try? decoder.decode(T.self, from: data) else { | |
fatalError("Failed to decode \(file) from bundle.") | |
} | |
return loaded | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment