Reflection icon indicating copy to clipboard operation
Reflection copied to clipboard

How to handle Default values?

Open mbalex99 opened this issue 7 years ago • 0 comments

What if I have a declaration of a struct like this?

struct Person {
    var firstName: String = "Max"
    var lastName: String = "Alexander"
    var age: Int = 12
    var phoneNumber: String = "4159300285"
}

But then I try to construct it using:

let person: Person = try! construct(dictionary: [:])

It'll tell me there are missing keys from the input dictionary. I understand the error. But what if the keys are missing and I prefer to construct it with their default values that I have declared above?

Is there any way to handle this sort of behavior?

mbalex99 avatar Oct 16 '18 21:10 mbalex99