I would like to assign the value element of a dictionary to a String variable.
In swiftUI, all value elements in dictionaries are returned as String? type.
When a String? type is assigned to a String variable, the String variable displays in the swiftUI app as Optional("theStringIamTryingToDisplay").
How do I get rid of the "Optional" in the app display?
You'll have to first make sure that the variable doesn't in fact contain nil
. You can do that in a number of ways -- might be good to read:
One way is by optional binding:
if let myVariable = myDictionary["key"] {
Text(myVariable)
}
Another is to provide an default value:
Text("My result: \(myDictionary["key"] ?? "default value")
There are other possibilities, but these, plus the links above, should get you started.