如何在Swift中获取数据,内部if/else语句



我在ViewControllerStruct中有两个pickerView,我在其中创建了一个函数,该函数将获取pickerViews的数据。在PickerViewdidSelectRow函数中,我编写了以下代码。

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView == fromPickerViewRolled {
let optionArray1 = currencyManager.currencyArrayOption1[row]
}
else if pickerView == toPickerViewRolled {

let optionArray2 = currencyManager.currencyArrayOption2[row]   
}
}

如何获取optionArray1和…的数据。。数组2在if/else之外,这样我就可以在函数中使用。如有任何帮助,我们将不胜感激。期待着答案。

您只需要在if语句块之外声明一个数组。像这样:

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
var currencyArray: [YourType] = []
if pickerView == fromPickerViewRolled {
currencyArray = currencyManager.currencyArrayOption1[row]
}
else if pickerView == toPickerViewRolled {
currencyArray = currencyManager.currencyArrayOption2[row]
}
dump(currencyArray)
}

最新更新