Stripe Alamofire JSON未填充数组



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

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

这是PaymentsVC.swift视图控制器中的代码:

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

最新更新