Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // Helper.swift
- // A small collection of quick helpers to avoid repeating the same old code.
- //
- // Created by Paul Hudson on 23/06/2019.
- // Copyright © 2019 Hacking with Swift. All rights reserved.
- //
- import UIKit
- extension Bundle {
- func decode<T: Decodable>(_ type: T.Type, from 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()
- guard let loaded = try? decoder.decode(T.self, from: data) else {
- fatalError("Failed to decode \(file) from bundle.")
- }
- return loaded
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement