Reputation: 77
I can not call the Array from Json, I get two points of error.
Can not assign a value of type '[CoinModel]' to type '[String]'
Can not assign value of type 'CoinModel' to type 'String?'
Where am I making mistakes?
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()
}
}
I get error on cell.nameLabel.text = coin[indexPath.row]
Cannot assign value of type 'CoinModel' to type 'String?'
and also on searchCoin = coin.filter({$0.name.prefix(searchText.count) == searchText})
Cannot assign value of type '[CoinModel]' to type '[String]'
Model:
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
}
}
Upvotes: 0
Views: 35
Reputation: 11242
It seems like you want to assign the name of the CoinModel
to the label.
cell.nameLabel.text = coin[indexPath.row].name
And for the mapping, if you want an array of CoinModel
names then you should be mapping the filtered result.
searchCoin = coin.compactMap( {$0.name.prefix(searchText.count) == searchText ? $0.name: nil} )
Tip: You should be using proper names to describe your variables. For example you have an array of CoinModel
which you have named coin
which instead could have been named coins
. Also, i would recommend that you maintain uniformity while storing your data source. One data source is an array of CoinModel
s while the other is an array of String
s. You should should have only one, preferably the CoinModel
imo.
Upvotes: 2