gpt4 book ai didi

swift - 如何完成完成 block - Swift/Stripe Payments

转载 作者:行者123 更新时间:2023-11-28 16:02:35 24 4
gpt4 key购买 nike

这是我的 block :

func createBackendChargeWithToken(_ token: STPToken, completion: @escaping STPTokenSubmissionHandler) {
if backendChargeURLString != "" {
if let url = URL(string: "https://wt-lightfalldev-gmail-com-0.run.webtask.io/stripe-payment/payment") {
let chargeParams : [String: AnyObject] = ["stripeToken": token.tokenId as AnyObject, "amount": shirtPrice as AnyObject]

Alamofire.request(url, method: .post, parameters: chargeParams, encoding: JSONEncoding.default, headers: heads).responseJSON { (response:DataResponse<Any>) in

switch(response.result) {
case .success(_):
if response.result.value != nil{
print(response.result.value!)
}
break

case .failure(_):
print(response.result.error!)
break
}
}
}
}
completion(STPBackendChargeResult.failure, NSError(domain: StripeDomain, code: 50, userInfo: [NSLocalizedDescriptionKey: "You created a token! Its value is \(token.tokenId!). Now configure your backend to accept this token and complete a charge."]))
}

但这需要在其中,但我不知道如何正确编写:

if (error) {  
completion(STPBackendChargeResultFailure, error);
} else {
completion(STPBackendChargeResultSuccess, nil);
}

代码始终使用 STPBackendChargeResult.failure 调用完成回调。所以它需要是一个 if 语句,我需要使用 failuresuccess 调用完成回调,具体取决于将 token 发送到后端的 Alamofire 请求的结果服务器。

最佳答案

为什么不能把它放在开关里呢?

switch(response.result) {
case .success(_):
if response.result.value != nil{
print(response.result.value!)
}
completion(STPBackendChargeResultSuccess, nil)
case .failure(_):
print(response.result.error!)
completion(STPBackendChargeResultFailure, error)
}

关于swift - 如何完成完成 block - Swift/Stripe Payments,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40792194/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com