Skip to content

Instantly share code, notes, and snippets.

@alfredoxyanez
Last active September 29, 2019 21:36
Show Gist options
  • Save alfredoxyanez/9dcbd4c6f9594a61155798fc740eeae2 to your computer and use it in GitHub Desktop.
Save alfredoxyanez/9dcbd4c6f9594a61155798fc740eeae2 to your computer and use it in GitHub Desktop.
import Foundation
class MessagesObject: ObservableObject {
@Published var messages: [Message] = []
init() {
self.messages = loadJson("MessagesJson.json")!
}
func loadJson(_ filename: String) -> [Message]? {
let data: Data
guard let file = Bundle.main.url(forResource: filename, withExtension: nil)
else {
fatalError("Couldn't find \(filename).")
}
do {
data = try Data(contentsOf: file)
} catch {
fatalError("Couldn't load \(filename).")
}
do {
let decoder = JSONDecoder()
return try decoder.decode([Message].self, from: data)
} catch {
fatalError("Couldn't parse \(filename) error: \(error)")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment