gpt4 book ai didi

swift - Stripe Alamofire JSON 不填充数组

转载 作者:行者123 更新时间:2023-11-28 14:04:37 25 4
gpt4 key购买 nike

我正在尝试使用 Stripe 中的信用卡列表填充 UITableView。我知道它适用于我的测试环境,因为我能够看到来自 Postman 的 JSON 响应。由于某种原因,它没有填充我的表。

因为这是一个 [Any Object] 我不需要创建一个单独的带有 init stings 的类吗?从 FireBase 提取信息后,我的应用程序中还有其他表填充数据并更新了 UILabel。

这是 PaymentsVC.swift View Controller 中的代码:

class PaymentVC: UIViewController, UITableViewDelegate, UITableViewDataSource {


@IBOutlet var headerView: UIView!
@IBOutlet var cardsTableView: UITableView!
@IBOutlet var cardTextField: STPPaymentCardTextField!

var stripeTool = StripeTools()
static let sharedClient = MyAPIClient()

//var customerId: String?
let customerId = "mycusid"

var baseURLString: String? = "https://api.sripe.com/v1/customers"
var baseURL: URL {
if let urlString = self.baseURLString, let url = URL(string: urlString) {
return url
} else {
fatalError()
}
}

var stripeUtil = StripeUtil()
var cards = [AnyObject]()



override func viewDidLoad() {
super.viewDidLoad()


}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


func numberOfSectionsInTableView(tableView: UITableView) -> Int {
//only one section
return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return self.cards.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let url = self.baseURL.appendingPathComponent("\(self.customerId)/sources?object=card")
let headers = ["Authorization": self.stripeTool.getBasicAuth()]
Alamofire.request(url, headers: headers)
.validate(statusCode: 200..<300)
.responseJSON { response in
switch response.result {
case .success(let result):
if let cards = STPCustomer.decodedObject(fromAPIResponse: result as? [String: AnyObject]) {
print(cards)
// completion(cards, nil)
} else {
// completion(nil, NSError.customerDecodingError)
}
case .failure(let error): break
// nil, error
}
}


//get card cell with cardCell identifier don't forget it on your storyboard
let cell = tableView.dequeueReusableCell(withIdentifier: "cardCell") as! CardCell

//get the last4 value on the card json, create the string and pass it to the label
if let last4 = self.cards[indexPath.row]["last4"] {
cell.cardNumberLabel.text = "**** **** **** \(last4!)"
}

//get the month/year expiration values on the card json, create the string and pass it to the label
if let expirationMonth = self.cards[indexPath.row]["exp_month"], let expirationYear = self.cards[indexPath.row]["exp_year"] {
cell.expirationLabel.text = "\(expirationMonth!)/\(expirationYear!)"
}
return cell
}

最佳答案

不要将 api 调用放在 cellForRowAt 中,因为它会在每次单元格创建/出队时调用,您需要将此代码放在 viewDidLoad 中,并重新加载表格

let url = self.baseURL.appendingPathComponent("\(self.customerId)/sources?object=card")
let headers = ["Authorization": self.stripeTool.getBasicAuth()]
Alamofire.request(url, headers: headers)
.validate(statusCode: 200..<300)
.responseJSON { response in
switch response.result {
case .success(let result):
if let cards = STPCustomer.decodedObject(fromAPIResponse: result as? [String: AnyObject]) {
print(cards)
self.cards = cards
self.cardsTableView.reloadData()
// completion(cards, nil)
} else {
// completion(nil, NSError.customerDecodingError)
}
case .failure(let error): break
// nil, error
}
}

不要忘记在viewDidLoad中设置

 self.cardsTableView.delegate = self
self.cardsTableView.dataSource = self

关于swift - Stripe Alamofire JSON 不填充数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53131658/

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