Last active
November 15, 2023 21:25
-
-
Save ayusinghi96/9d304d703be7eb4f633768c60d887bb0 to your computer and use it in GitHub Desktop.
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 | |
/// | |
/// A file reader class. | |
/// | |
/// - Note: Here we will decode JSON data from a static file contaning Movies. | |
/// | |
final class FileReader { | |
public static func getMovies( | |
callback: @escaping (Result<[Movie], FileReaderError>) -> Void) | |
{ | |
guard let bundleUrlString = Bundle.main.url(forResource: "Movies", withExtension: "json") | |
else { return } | |
do { | |
let data = try Data(contentsOf: bundleUrlString) | |
let movies = try JSONDecoder().decode([Movie].self, from: data) | |
callback(.success(movies)) | |
} catch { | |
callback(.failure(FileReaderError())) | |
} | |
} | |
} | |
/// | |
/// Error class for file reader. | |
/// | |
class FileReaderError: Error { | |
init () {} | |
func presentError(withKey key: String) | |
{ | |
print("Error:" + key) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment