[SwiftUI] Binding<String> in AF Request URL

Hi together,
i have a Binding in my SwiftUI View. It looks like this:

struct ContentView: View {
    var body: some View {
        let binding = Binding<String>(get: { () -> String in
            return String(format: "%.2f", self.model.value)
        }) { (s) in
            var s = s
            s.removeAll { (c) -> Bool in
                !c.isNumber
            }
            self.model.txt = s
        }
        VStack{
            TextField("Amount", text: binding)
            Button(action: {
                loading = true
                
                AF.request("https://example.com/\(binding)", method: .get)
                    .responseString{response in
                        // ...
                    }
            }){
                Text("Go")
            }
        }
    }
}

But I can't use the binding in my Interpolation - it crashes instantly when this AF Request code runs!
Can I "refactor" the binding to a default var?

Can anyone help me?

Thanks!

The type of binding is Binding<String>. What you want in the string interpolation is the wrapped value of the binding, which is of type String:

AF.request("https://example.com/\(binding.wrappedValue)", method: .get)
Terms of Service

Privacy Policy

Cookie Policy