Guest User

Untitled

a guest
Dec 14th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1. extension Bundle {
  2. func decode<T: Decodable>(_ type: T.Type, from filename: String) -> T {
  3. guard let json = url(forResource: filename, withExtension: nil) else {
  4. fatalError("Failed to locate \(filename) in app bundle.")
  5. }
  6.  
  7. guard let jsonData = try? Data(contentsOf: json) else {
  8. fatalError("Failed to load \(filename) from app bundle.")
  9. }
  10.  
  11. let decoder = JSONDecoder()
  12.  
  13. guard let result = try? decoder.decode(T.self, from: jsonData) else {
  14. fatalError("Failed to decode \(filename) from app bundle.")
  15. }
  16.  
  17. return result
  18. }
  19. }
  20.  
  21. let items = Bundle.main.decode([TourItem].self, from: "Tour.json")
Add Comment
Please, Sign In to add comment