I have a jsondata struct that is build from a JSON file that contains this :
{
inputFieldTitle: "Total Cost",
keyboardType: "numberPad"
}
In my code, how can I use the attribute keyboardType to initialize the UIKeyboardType enum in order to set the keyboard type on my TextField?
Normally we would do this to get a numberPad keyboard (SwiftUI) :
TextField(jsondata.inputFieldTitle) { ... }
.keyboardType(.numberPad)
But in my scenario, I can't hardcode the keyboardType with .numberPad, I have to use what's specified in the jsondata, how can I use that value to set the keyboardType on the TextField? This obvisouly does not work because the UIKeyboardType is of type Int :
TextField(jsondata.inputFieldTitle) { ... }
.keyboardType(UIKeyboardType(rawValue: jsondata.keyboardType))
Any tips? Thanks.
UIKeyboardType
is-aInt
, so you cannot userawValue
, because your json value is string.Here is possible solution
and you can now use it as