gpt4 book ai didi

ios - TableView 中的搜索栏无法调用数组

转载 作者:行者123 更新时间:2023-11-28 11:42:36 29 4
gpt4 key购买 nike

我无法从 Json 调用数组,我得到两点错误。

Can not assign a value of type '[CoinModel]' to type '[String]'

Can not assign value of type 'CoinModel' to type '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] 上遇到错误

Cannot assign value of type 'CoinModel' to type 'String?'

还有 searchCoin = coin.filter({$0.name.prefix(searchText.count) == searchText})

Cannot assign value of type '[CoinModel]' to type '[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
}


}

最佳答案

您似乎想将 CoinModel 的名称分配给标签。

cell.nameLabel.text = coin[indexPath.row].name

对于映射,如果您想要一个 CoinModel 名称数组,那么您应该映射过滤后的结果。

searchCoin = coin.compactMap( {$0.name.prefix(searchText.count) == searchText ? $0.name: nil} )

提示:您应该使用专有名称来描述您的变量。例如,您有一个 CoinModel 数组,您将其命名为 coin,而它本可以被命名为 coins。另外,我建议您在存储数据源时保持一致性。一个数据源是 CoinModel 数组,而另一个是 String 数组。你应该只有一个,最好是 CoinModel imo。

关于ios - TableView 中的搜索栏无法调用数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53350401/

29 4 0