Skip to content

Instantly share code, notes, and snippets.

@ayusinghi96
Last active November 15, 2023 21:25
Show Gist options
  • Save ayusinghi96/9d304d703be7eb4f633768c60d887bb0 to your computer and use it in GitHub Desktop.
Save ayusinghi96/9d304d703be7eb4f633768c60d887bb0 to your computer and use it in GitHub Desktop.
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