将JSON整数转换为字符串时遇到麻烦(Swift 5)

问题描述 投票:0回答:1

是Swift的新手,通常是编码。尝试将JSON对象数组放入tableView中。在tableView委托方法的detailTextView.text中将我的Ints转换为String时遇到麻烦。收到错误消息“ Initializer'init(_ :)'要求输入'Int?'”遵循“ LosslessStringConvertible。”“尝试使用它,但这是一个错误的小漏洞。一天大部分时间都在浏览,但是没有运气。

class AllCountriesVC: UITableViewController {


    struct CovidData: Codable {
        let country: String
        let cases: Int?
        let todayCases: Int?
        let deaths: Int?
        let todayDeaths: Int?
        let recovered: Int?
        let active: Int?
        let critical: Int?
        let totalTests: Int?
    }


    var data = [CovidData]()

    override func viewWillAppear(_ animated: Bool) {
        load()
        self.tableView.reloadData()
    }


    override func viewDidLoad() {
        super.viewDidLoad() 
    }

    func load() {
        if let url = URL(string: "https://coronavirus-19-api.herokuapp.com/countries/") {
        let jsonData = try! Data(contentsOf: url)
            self.data = try! JSONDecoder().decode([CovidData].self, from: jsonData)

            self.tableView.reloadData()
        }
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return data.count ?? 1       
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
        let countryData = data[indexPath.row]
        cell.textLabel?.text = countryData.country
        cell.detailTextLabel?.text = String(countryData.cases)
        //this is where it fails with error "Initializer 'init(_:)' requires that 'Int?' conform to 'LosslessStringConvertible'"

        return cell
    } 
}
ios json swift type-conversion
1个回答
0
投票

[通过查看数据,唯一具有空数据的属性是recovered,因此将其余属性更改为非可选。这将使您的工作变得容易得多。

我也建议您使用do/try/catch而不是try!

class AllCountriesVC: UITableViewController {
    struct CovidData: Codable {
        let country: String
        let cases: Int
        let todayCases: Int
        let deaths: Int
        let todayDeaths: Int
        let recovered: Int?
        let active: Int
        let critical: Int
        let totalTests: Int
    }


    var data = [CovidData]()

    override func viewWillAppear(_ animated: Bool) {
        load()
    }

    func load() {
        if let url = URL(string: "https://coronavirus-19-api.herokuapp.com/countries/") {
            do {
                let jsonData = try Data(contentsOf: url)
                self.data = try JSONDecoder().decode([CovidData].self, from: jsonData)

                self.tableView.reloadData()
            }
            catch {
                print(error)
            }
        }
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return data.count ?? 1       
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
        let countryData = data[indexPath.row]
        cell.textLabel?.text = countryData.country
        cell.detailTextLabel?.text = String(countryData.cases)
        return cell
    } 
}

如果您确实想使用可选的内容,请说recovered,那么您可以使用nil合并运算符-??

String(countryData.recovered ?? 0)
© www.soinside.com 2019 - 2024. All rights reserved.