【问题标题】:Value of optional type 'BTAPIClient?' must be unwrapped to a value of type 'BTAPIClient'可选类型 'BTAPIClient?' 的值必须解包为“BTAPIClient”类型的值
【发布时间】:2022-01-05 02:56:54
【问题描述】:

我正在使用 Paypal-Braintree 的官方教程进行支付网关。

我按照教程仍然报错。

我的 viewcontroller 代码有错误 -

   import UIKit
    import Braintree
    
     class MyViewController: UIViewController {

    var braintreeClient: BTAPIClient?

   override func viewDidLoad() {
    super.viewDidLoad()


    // Example: Initialize BTAPIClient, if you haven't already
    braintreeClient = BTAPIClient(authorization: "sandbox_bnkgmk59_5dxr37xvb54mg8sp ")
 
       }

      @IBAction func clickedBtnPay(_ sender: Any) {
      let payPalDriver = BTPayPalDriver(apiClient:braintreeClient) ***/*Error - i) Value of optional type 'BTAPIClient?' must be unwrapped to a value of type 'BTAPIClient'

ii) 使用 '??' 合并当可选值包含“nil”时提供默认值 iii) 使用 '!' 强制展开如果可选值包含“nil”则中止执行/**

            payPalDriver.viewControllerPresentingDelegate = self
            payPalDriver.appSwitchDelegate = self // Optional
    // Specify the transaction amount here. "2.32" is used in this example.
    let request = BTPayPalRequest(amount: "2.32")
    request.currencyCode = "USD" // Optional; see BTPayPalRequest.h for more options

    payPalDriver.requestOneTimePayment(request) { (tokenizedPayPalAccount, error) in
        if let tokenizedPayPalAccount = tokenizedPayPalAccount {
            print("Got a nonce: \(tokenizedPayPalAccount.nonce)")

            // Access additional information
            let email = tokenizedPayPalAccount.email
            let firstName = tokenizedPayPalAccount.firstName
            let lastName = tokenizedPayPalAccount.lastName
            let phone = tokenizedPayPalAccount.phone

            // See BTPostalAddress.h for details
            let billingAddress = tokenizedPayPalAccount.billingAddress
            let shippingAddress = tokenizedPayPalAccount.shippingAddress
        } else if let error = error {
            // Handle error here...
        } else {
            // Buyer canceled payment approval
        }


}
  }
     }
    extension MyViewController:BTViewControllerPresentingDelegate{
    func paymentDriver(_ driver: Any, requestsPresentationOf viewController:     UIViewController) {
    
     }
  
     func paymentDriver(_ driver: Any, requestsDismissalOf viewController: UIViewController) {
    
         }
     }

  extension MyViewController: BTAppSwitchDelegate {
   func appSwitcherWillPerformAppSwitch(_ appSwitcher: Any) {
    
}

func appSwitcher(_ appSwitcher: Any, didPerformSwitchTo target: BTAppSwitchTarget) {
    
}

func appSwitcherWillProcessPaymentInfo(_ appSwitcher: Any) {
    
   }
  }

当我遵循错误中提到的建议时,它会显示更多错误。如何解决?

【问题讨论】:

  • 如果不展示如何在 IBAction 中修改代码,很难说。
  • 错误很明显,您不能将可选值传递给该函数。您需要先使用if let 或guard let 解开它
  • 当我添加感叹号时,构建成功 - “让 payPalDriver = BTPayPalDriver(apiClient:braintreeClient!)”,但是当我按下按钮时它显示错误 i) 线程 1:致命错误:意外在展开 Optional 值时发现 nil。 ii) xcode 控制台显示错误 - “[BraintreeSDK] 错误 BTClient 无法初始化,因为提供的 clientToken 无效”

标签: ios swift xcode paypal-sandbox


【解决方案1】:

我在这里犯了两个愚蠢的错误- i)当我添加一个感叹号时,构建成功 - “让 payPalDriver = BTPayPalDriver(apiClient:braintreeClient!)” ii) xcode 控制台显示错误 - “[BraintreeSDK] 错误 BTClient 无法初始化,因为提供的 clientToken 无效” - 我没有做的是在密钥中留下一个空白 - 我已经完成了 - braintreeClient = BTAPIClient(authorization: "sandbox_bsdfsfdfsg59_5dfgxr37xvb54mg8sp ") 即密钥后的空白。我纠正了它,即 -braintreeClient = BTAPIClient(authorization: "sandbox_bsdfsfdfsg59_5dfgxr37xvb54mg8sp") - 没有空格,一切顺利。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-14
    • 1970-01-01
    相关资源
    最近更新 更多