SwiftUI:为什么这个布尔值不改变它的值?
我尝试更改此函数中的绑定布尔值。打印按预期工作(因此交易信息在控制台中正确显示),但 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()
}
我对编码
还很陌生——我在这里错过了什么?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么要将会话放在自己的
struct: View
中,这并不是真正的视图。您可以直接将绑定传递给 func(无论您喜欢在哪里)。这是一个例子:
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: