[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
            self.model.txt = s
            TextField("Amount", text: binding)
            Button(action: {
                loading = true
                AF.request("https://example.com/\(binding)", method: .get)
                    .responseString{response in
                        // ...

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?


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