Last active
September 29, 2019 21:36
-
-
Save alfredoxyanez/9dcbd4c6f9594a61155798fc740eeae2 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 | |
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