I have a large number of structs that are being populated by decoding JSON data. I want the system to be robust and handle optional keys in the JSON. This leaves me with a lot of optional unwrapping code in my UI.
struct SomeStruct: Decodable {
public var id: Int
public var firstName: String?
public var lastName: String?
}
Text("\(someStruct.lastName ?? ""), \(someStruct.firstName ?? "")")
I am looking for a clean way to set defaults and avoid unwrapping theses optionals throughout my UI layer. For example someone gave this solution: https://stackoverflow.com/a/57260118. This would work but would also create a ton of near duplicate code. Does anyone have a more elegant solution to this? The success criteria is JSON -> struct parsing with a minimal required keys and removing optional unwrapping from the UI code.