Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extension Bundle {
- func decode<T: Decodable>(_ type: T.Type, from filename: String) -> T {
- guard let json = url(forResource: filename, withExtension: nil) else {
- fatalError("Failed to locate \(filename) in app bundle.")
- }
- guard let jsonData = try? Data(contentsOf: json) else {
- fatalError("Failed to load \(filename) from app bundle.")
- }
- let decoder = JSONDecoder()
- guard let result = try? decoder.decode(T.self, from: jsonData) else {
- fatalError("Failed to decode \(filename) from app bundle.")
- }
- return result
- }
- }
- let items = Bundle.main.decode([TourItem].self, from: "Tour.json")
Add Comment
Please, Sign In to add comment