Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json 如何从pickerview获取某个值的索引?_Json_Swift_Sorting - Fatal编程技术网

Json 如何从pickerview获取某个值的索引?

Json 如何从pickerview获取某个值的索引?,json,swift,sorting,Json,Swift,Sorting,我的应用程序试图显示一个国家的新冠病毒感染排名(covid19api.com)。 我有一个pickerview来选择国家和基本的新冠病毒信息。 我根据感染人数对数据库进行了分类。 我的问题是如何获得索引值 基于pickerview选择的国家/地区的排序数组的名称 尝试使用“firstIndex”,但出现以下错误: 在“StringProtocol”上引用运算符函数“==”需要“BasicVidInfoParams”符合“StringProtocol” 以下是该项目的代码: struct Basi

我的应用程序试图显示一个国家的新冠病毒感染排名(covid19api.com)。 我有一个pickerview来选择国家和基本的新冠病毒信息。 我根据感染人数对数据库进行了分类。 我的问题是如何获得索引值 基于pickerview选择的国家/地区的排序数组的名称

尝试使用“firstIndex”,但出现以下错误: 在“StringProtocol”上引用运算符函数“==”需要“BasicVidInfoParams”符合“StringProtocol”

以下是该项目的代码:

struct BasicCovidInfo: Decodable {
    var Countries: [BasicCovidInfoParams]
}
struct BasicCovidInfoParams: Decodable {
    var Country: String?
    var NewConfirmed: Int?
    var TotalConfirmed: Int?
    var NewDeaths: Int?
    var TotalDeaths: Int?
    var NewRecovered: Int?
    var TotalRecovered: Int?
    var Date: String?
}


class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
     
    //var basiccountrycovidinfo = BasicCovidInfoParams()
    var basiccountrycovidinfo = [BasicCovidInfoParams]()
    
    @IBOutlet weak var rankFirstLbl: UILabel!
    @IBOutlet weak var rankSecondLbl: UILabel!
    @IBOutlet weak var rankThirdLbl: UILabel!
    @IBOutlet weak var rankingLbl: UILabel!
    
    
    @IBOutlet weak var covid19CountryPicker: UIPickerView!
    @IBOutlet weak var totalConfirmedLbl: UILabel!
    @IBOutlet weak var totalDeathsLbl: UILabel!
    @IBOutlet weak var totalRecoverdLbl: UILabel!
    @IBOutlet weak var toDateLbl: UILabel!
    
   
    override func viewDidLoad() {
        super.viewDidLoad()

        covid19CountryPicker.delegate = self
        covid19CountryPicker.dataSource = self
        
        getRemoteCovidJsonFile()
        
    }
    
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return basiccountrycovidinfo.count
    }
    
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        
        let numberFormatter = NumberFormatter()
        numberFormatter.numberStyle = NumberFormatter.Style.decimal
        totalConfirmedLbl.text = numberFormatter.string(from: NSNumber(value: basiccountrycovidinfo[row].TotalConfirmed!))
        totalDeathsLbl.text = numberFormatter.string(from: NSNumber(value: basiccountrycovidinfo[row].TotalDeaths!))
        totalRecoverdLbl.text = numberFormatter.string(from: NSNumber(value: basiccountrycovidinfo[row].TotalRecovered!))
        
        
        let dataString = String(basiccountrycovidinfo[row].Date!)
        let from = dataString.index(dataString.startIndex, offsetBy:0)
        let to = dataString.index(dataString.startIndex, offsetBy:10)
        let newString = String(dataString[from..<to])
        //print(newString)
        toDateLbl.text = "Last Update : " + newString

        return basiccountrycovidinfo[row].Country
    }

    
    func getRemoteCovidJsonFile() {
        
        //Info source = "https://api.covid19api.com/summary"
        if let jsonUrlString = URL(string: "https://api.covid19api.com/summary") {
            URLSession.shared.dataTask(with: jsonUrlString) { [self] (data, response, error) in
                
                if let data = data {
                    do {
                        let decoder = JSONDecoder()
                        //decoder.keyDecodingStrategy = .convertFromSnakeCase
                        let covidDataInfo = try decoder.decode(BasicCovidInfo.self, from: data)
                        //print(covidDataInfo.Countries[1].Country!)
                        self.basiccountrycovidinfo = covidDataInfo.Countries
                        
                        let array1 = covidDataInfo.Countries.sorted { (a, b) -> Bool in
                            return a.TotalConfirmed! > b.TotalConfirmed!
                        }

                        let selectedText = pickerView(covid19CountryPicker, titleForRow: 0, forComponent: 0)
                        if let indexOfSelectedText = array1.firstIndex{$0 == selectedText!} {
                            print(array1[indexOfSelectedText])
                        }
               
                        DispatchQueue.main.async {
                            rankFirstLbl.text = array1[0].Country
                            rankSecondLbl.text = array1[1].Country
                            rankThirdLbl.text = array1[2].Country
                    
                            covid19CountryPicker.reloadAllComponents()
        
                        }
                        
                    } catch {
                        print(error.localizedDescription)
                    }
                }
                
            }.resume()
        }

    }
 
}
struct basicVidInfo:可解码{
var国家:[BasicCovidInfoParams]
}
结构BasicVidInfoParams:可解码{
国家:字符串?
变量:Int?
变量:Int?
变量:Int?
总死亡数:整数?
var:Int?
var:Int?
变量日期:字符串?
}
类ViewController:UIViewController、UIPickerViewDeleteGate、UIPickerViewDataSource{
//var basiccountrycovidinfo=BasicCovidInfoParams()
var basiccountrycovidinfo=[BasicCovidInfoParams]()
@IBVAR rankFirstLbl:UILabel!
@IBOutlet弱var rankSecondLbl:UILabel!
@IBOutlet弱var rankThirdLbl:UILabel!
@IBL弱var rankingLbl:UILabel!
@IBOUTLE弱var covid19CountryPicker:UIPickerView!
@IBOutlet弱var totalConfirmedLbl:UILabel!
@IBVAR弱var totalDeathsLbl:UILabel!
@IBOutlet弱var totalRecoverdLbl:UILabel!
@IBL弱var ToDatelebl:UILabel!
重写func viewDidLoad(){
super.viewDidLoad()
covid19CountryPicker.delegate=self
covid19CountryPicker.dataSource=self
getRemoteCovidJsonFile()
}
func numberOfComponents(在pickerView:UIPickerView中)->Int{
返回1
}
func pickerView(pickerView:UIPickerView,numberOfRowsInComponent:Int)->Int{
返回basiccountrycovidinfo.count
}
func pickerView(pickerView:UIPickerView,titleForRow行:Int,forComponent组件:Int)->String{
设numberFormatter=numberFormatter()
numberFormatter.numberStyle=numberFormatter.Style.decimal
totalConfirmedLbl.text=numberFormatter.string(from:NSNumber(值:basiccountrycovidinfo[row].TotalConfirmed!))
totalDeathsLbl.text=numberFormatter.string(发件人:NSNumber(值:basiccountrycovidinfo[row].TotalDeathES!))
totalRecoverdLbl.text=numberFormatter.string(from:NSNumber(值:basiccountrycovidinfo[row].TotalRecovered!))
让dataString=String(basiccountrycovidinfo[row].Date!)
let from=dataString.index(dataString.startIndex,偏移量:0)
let to=dataString.index(dataString.startIndex,offsetBy:10)
让newString=String(dataString[from..Bool in
返回a.totalconfirm!>b.totalconfirm!
}
让selectedText=pickerView(covid19CountryPicker,titleForRow:0,forComponent:0)
如果让indexOfSelectedText=array1.firstIndex{$0==selectedText!}{
打印(数组1[indexOfSelectedText])
}
DispatchQueue.main.async{
rankFirstLbl.text=array1[0]。国家/地区
rankSecondLbl.text=array1[1]。国家/地区
rankThirdLbl.text=array1[2]。国家/地区
covid19CountryPicker.reloadAllComponents()
}
}抓住{
打印(错误。本地化描述)
}
}
}1.简历()
}
}
}

您可以使用
UIPickerView
selectedRow
功能获取所选行


一旦您获得选取的选取器索引值,您就可以使用index属性从数组中选取值。

加载数据后,您的代码显示正常,并将!unsorted!版本提供给UIPicker以供显示。然后事情就出了问题。在将数据提供给UIPicker之前,您似乎试图选取一些不会产生错误的文本一切都很有用。然后你从排序数组中以一种非常不协调的方式设置一些标签。我认为你想等待UIPicker的操作,并根据所选国家的详细信息设置标签,但你不了解UIPicker的工作原理。请阅读其文档并提供排序数据。非常感谢.您的建议使我更改了方法。我已更改了方法,先对其进行排序,然后再将其分配给pickerview。
func selectedRow(inComponent component: Int) -> Int