我无法从Json调用Array,我得到了两点错误。
无法将类型为[[CoinModel]'的值分配为类型为[[String]]
无法将类型为“ CoinModel”的值分配为类型为“ String”的
我在哪里犯错?
import UIKit
import Alamofire
import SwiftyJSON
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate {
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var searchBar: UISearchBar!
let URL_DATA = "https://api.coincap.io/v2/assets"
var coin = [CoinModel]()
var searchCoin = [String]()
var searching = false
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if searching {
return searchCoin.count
} else {
return coin.count
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CoinTableViewCell
tableView.backgroundColor = .clear
cell.backgroundColor = .clear
if searching{
cell.nameLabel.text = searchCoin[indexPath.row]
} else {
cell.nameLabel.text = coin[indexPath.row]
}
let coindata: CoinModel
coindata = coin[indexPath.row]
cell.nameLabel.text = coindata.name
cell.symbolLabel.text = coindata.symbol
let priceFloat = (coindata.price as NSString).floatValue
let costString = String(format:"%.3f", priceFloat)
cell.priceLabel.text = "\(costString)"
let percentFloat = (coindata.percent as NSString).floatValue
let percentString = String(format:"%.2f", percentFloat)
if percentFloat < 0 {
cell.percentLabel.text = "\(percentString)%"
cell.percentLabel.textColor = UIColor(hexString: "#F47E99")
} else {
cell.percentLabel.text = "+\(percentString)%"
cell.percentLabel.textColor = UIColor(hexString: "#19984A")
}
return cell
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
searchCoin = coin.filter({$0.name.prefix(searchText.count) == searchText})
searching = true
tableView.reloadData()
}
func getCoinData() {
Alamofire.request(URL_DATA).responseJSON { response in
if let json = response.result.value as? [String: Any], let arr = json["data"] as? [[String: Any]] {
let CoinArray: [[String: Any]] = arr
for dict in CoinArray {
if let name = dict["name"] as? String,
let symbol = dict["symbol"] as? String,
let price = dict["priceUsd"] as? String,
let percent = dict["changePercent24Hr"] as? String
{
let coinnModal: CoinModel = CoinModel(name: name, symbol: symbol, price: price, percent: percent)
self.coin.append(coinnModal)
}
self.tableView.reloadData()
}
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
getCoinData()
}
}
我在cell.nameLabel.text = coin[indexPath.row]
上遇到错误
无法将类型为“ CoinModel”的值分配为类型为“字符串?”
以及searchCoin = coin.filter({$0.name.prefix(searchText.count) == searchText})
无法将类型“ [CoinModel]”的值分配为类型“ [String]”
型号:
import Foundation
class CoinModel {
var name: String
var symbol: String
var price: String
var percent: String
init(name: String, symbol: String, price: String, percent: String) {
self.name = name
self.symbol = symbol
self.price = price
self.percent = percent
}
}
答案 0 :(得分:2)
似乎您想将CoinModel
的名称分配给标签。
cell.nameLabel.text = coin[indexPath.row].name
对于映射,如果要使用CoinModel
个名称数组,则应该映射过滤后的结果。
searchCoin = coin.compactMap( {$0.name.prefix(searchText.count) == searchText ? $0.name: nil} )
提示:您应该使用专有名称来描述变量。例如,您有一个名为CoinModel
的{{1}}数组,而该数组可能已命名为coin
。另外,我建议您在存储数据源时保持一致性。一个数据源是coins
的数组,而另一个是CoinModel
的数组。您应该只有一个,最好是String
imo。