In SwiftUI I’d like to store some key-value pair data in a dictionary. Can I bind an item in the dictionary to TextField or elsewhere that requires Binding?
I want to do something like this:
@State var myDic: [String : String] = [
"name": "Tim"
"company": "Apple"
]
TextField("Name", text: $myDic["name"] ?? "")
where the behavior is:
- If the “name” exists in dictionary:
- User editing in TextField will change the value.
- User deleting the contents in TextField (setting it as “”), the dictionary removes the item of “name”.
- If the “name” not exists in dictionary:
- The TextField display as empty (“”).
- User set value will let dictionary add that item and assign value.