【发布时间】:2018-06-10 12:12:24
【问题描述】:
我正在尝试将 func getCoinData 中的数据保存到数组 sympolsCoin 和数组 sympolsCoin 以在我的 TableView 中使用它
我在同一个 ViewController.swift 文件中创建了这个类:
struct Coin: Decodable {
let symbol : String
let price_usd : String }
这在我的视图控制器类中:
var coins = [Coin]()
var sympolsCoin = [String]()
var priceUSDcoin = [String]()
func getCoinData(completion: @escaping () -> ()) {
let jsonURL = "https://api.coinmarketcap.com/v1/ticker/"
let url = URL(string: jsonURL)
URLSession.shared.dataTask(with: url!) { (data, response, error) in
do {
self.coins = try JSONDecoder().decode([Coin].self, from: data!)
for info in self.coins {
self.sympolsCoin.append(info.symbol)
self.priceUSDcoin.append(info.price_usd)
print("\(self.sympolsCoin) : \(self.priceUSDcoin)")
completion()
}
}
catch {
print("Error is : \n\(error)")
}
}.resume()
}
当我在 TableView 中使用数组时,我得到了空白表!
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "BitcoinTableViewCell", for: indexPath) as! BitcoinTableViewCell
cell.coinNameLable.text = sympolsCoin[indexPath.row]
cell.priceLable.text = priceUSDcoin[indexPath.row]
return cell
}
【问题讨论】:
标签: json swift uitableview tableview swift4