SwiftUI:为什么这个布尔值不改变它的值?

发布于 2025-01-13 12:38:38 字数 884 浏览 4 评论 0 原文

我尝试更改此函数中的绑定布尔值。打印按预期工作(因此交易信息在控制台中正确显示),但 bool 值(即 var successPayment 的值)没有改变。我也尝试在打印“付款成功”后打印这个bool的值,但它总是错误的。

注:支付确实成功!

struct CheckView: View {

@Binding var successfulPayment: Bool

func getTransactionInformation () {

    guard let url = URL(string: "URL-HERE") else {return}

    URLSession.shared.dataTask(with: url){ [self] data, _, _ in
        let transactioninformation = try! JSONDecoder().decode(IDValues.TransactionInformation.self, from: data!)

        print(transactioninformation)

        if (transactioninformation.id == transactionId && transactioninformation.statuscode == "Success") {
            
            successfulPayment = true
            
            print("Payment was successful!")
            
        } else {
            
            }

    }
    .resume()
    
}

我对编码

还很陌生——我在这里错过了什么?

I try to change a binding bool in this function. Printing works as expected (so the transaction information is displayed correctly in the console), but the bool value - so the value of the var successfulPayment - isn't changing. I also tried to print the value of this bool after the print of "Payment was successful", but it was always false.

Note: the payment really is successful!

struct CheckView: View {

@Binding var successfulPayment: Bool

func getTransactionInformation () {

    guard let url = URL(string: "URL-HERE") else {return}

    URLSession.shared.dataTask(with: url){ [self] data, _, _ in
        let transactioninformation = try! JSONDecoder().decode(IDValues.TransactionInformation.self, from: data!)

        print(transactioninformation)

        if (transactioninformation.id == transactionId && transactioninformation.statuscode == "Success") {
            
            successfulPayment = true
            
            print("Payment was successful!")
            
        } else {
            
            }

    }
    .resume()
    
}

}

I am pretty new to coding - what am I missing here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

为什么要将会话放在自己的 struct: View 中,这并不是真正的视图。
您可以直接将绑定传递给 func(无论您喜欢在哪里)。这是一个例子:

struct ContentView: View {
    
    @State private var successfulPayment: Bool = false
    
    var body: some View {
        Form {
            Text("Payment is \(successfulPayment ? "successful" : "pending")")
            Button("Pay") {
                getTransactionInformation($successfulPayment)
            }
        }
    }
    
    func getTransactionInformation (_ success: Binding<Bool>) {
        DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
            success.wrappedValue = true
        }
    }
}

Why do you put the session in an own struct: View which isn't really a view.
You can pass the binding to the func directly (wherever you prefer it to be). Here is an example:

struct ContentView: View {
    
    @State private var successfulPayment: Bool = false
    
    var body: some View {
        Form {
            Text("Payment is \(successfulPayment ? "successful" : "pending")")
            Button("Pay") {
                getTransactionInformation($successfulPayment)
            }
        }
    }
    
    func getTransactionInformation (_ success: Binding<Bool>) {
        DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
            success.wrappedValue = true
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文