I have to initialise values by reading from JSON file hence was writing failable initiazer. Please review following code and provide suggestions
struct ViewModel {
var cards : EmployeeData
init?() {
if let path = Bundle.main.path(forResource: "EmployeeData", ofType: "json") {
do {
let jsonData = try Data(contentsOf: URL(fileURLWithPath: path), options: .mappedIfSafe)
let decoder = JSONDecoder()
self.cards = try decoder.decode(EmployeeData.self, from: jsonData)
}
catch {
print(error.localizedDescription)
return nil
}
}
}
}
struct EmployeeData : Decodable {
var employeeCards : [EmployeeCards]
struct EmployeeCards : Decodable {
var title : String
var description : String
}
}