如何在Swift中从字典加载数据到PickerView中?
我有一个带有String
键(州)和Double
值(税金)的字典,我想做的是能够将键设置为UIPickerView的titleForRow
并在选定key
的情况下显示其值在textField
中。
// Ideal Dictionary
private var states:[String:Double] = ["Alabama":6.000, "Illinois":7.000, "Oregon":8.000, "Wisconsin":9.000]
我可以使用两个数组来完成上述描述的操作,一个用于状态,一个用于税收,但我不认为从字典中加载不可能。
以下是我目前使用两个数组实现的代码。
// Arrays
private var stateName = ["Alabama", "Illinois", "Oregon", "Wisconsin"]
private var stateTax = ["6.000", "7.000", "8.000", "9.000"]
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return stateName.count
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
inputTaxRate.text = stateTax[row]
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return stateName[row]
}
有什么建议可以改进上述代码吗?
private let stateInfo: [(name: String, tax: Double)] = [ ("Alabama", 6.000), ("Illinois", 7.000), ("Oregon", 8.000), ("Wisconsin", 9.000) ] var tax:Array->(name:String, age:Int){ get{ return stateInfo } }
}` 我该如何从类中返回数组? - fs_tigrevar tax:[(name: String, tax: Double)] { get { return stateInfo } }
吗? - vacawama