I'm using the following :
(https://developer.apple.com/forums/thread/670303)
The little issue I'm facing now, is to display the dictionary. I'm using SwiftUI for my app UI.
Here is the code:
This is the error I'm having: Cannot convert value of type 'String?' to expected argument type 'Binding<String>'
If I unwrap this way :
I get this error: Cannot convert value of type 'String' to expected argumument type 'Binding<String>'
I'm getting the binding value data from what is on the NDEF tag
(https://developer.apple.com/forums/thread/670303)
Code Block Swift var data = "Key0:Value\nKey1:Value\nKey2:Value\nKey3:Value\n" var deciphered = data.split(separator: "\n").reduce(into: [String: String]()) { let str = $1.split(separator: ":") if let first = str.first, let value = str.last { $0[String(first)] = String(value) } }
The little issue I'm facing now, is to display the dictionary. I'm using SwiftUI for my app UI.
Here is the code:
Code Block Swift struct NFCMobileView: View { @Binding var data: String var body: some View { var deciphered = data.split(separator: "\n").reduce(into: [String: String]()) { let str = $1.split(separator: ":") if let first = str.first, let value = str.last { $0[String(first)] = String(value) } } HStack { Text("Last Name") TextField("", text: deciphered["lastName"]) /* error */ } } }
This is the error I'm having: Cannot convert value of type 'String?' to expected argument type 'Binding<String>'
If I unwrap this way :
Code Block Swift TextField("", text: deciphered["lastName"] ?? "")
I get this error: Cannot convert value of type 'String' to expected argumument type 'Binding<String>'
I'm getting the binding value data from what is on the NDEF tag
Thanks for showing your pseudo code, that makes your intention clearer.If I would need to access different value from my dictionary (like the code snippet here-under)
First of all, you need to pass Binding<String> to text: in TextField.init(_:_text:).
deciphered["lastName"] is of type String?, and deciphered["lastName"] ?? "" is of type String.
Neither of them are of type Binding<String>.
And one more important thing, the original value storage for the passed Binding should not be a local variable.
Please try this:
Code Block struct NFCMobileView: View { @Binding var data: String @State var deciphered: [String: String] = [:] //<- Make this a Dictionary type var body: some View { //Text(deciphered.description) //Uncomment for debugging Form { Group { HStack { Text("Last name") TextField("", text: _deciphered.binding("lastName")) //<- } HStack { Text("First name") TextField("", text: _deciphered.binding("firstName")) //<- } HStack { Text("Gender") TextField("", text: _deciphered.binding("gender")) //<- } HStack { Text("Age") TextField("", text: _deciphered.binding("Key3")) //<- (`"Key3"` is the right key? } } } .onAppear { //↓ No `var` here deciphered = data.split(separator: "\n").reduce(into: [String: String]()) { let str = $1.split(separator: ":") if let first = str.first, let value = str.last { $0[String(first)] = String(value) } } } } }
Sorry, almost forgotten. You need the following extension to create a Binding<String> from an @State dictionary.
Code Block extension State where Value == Dictionary<String, String> { /// Returns Binding for non-Optional func binding(_ key: String) -> Binding<String> { return Binding<String>( get: {self.wrappedValue[key] ?? ""}, set: {self.wrappedValue[key] = $0} ) } }