Вы можете попробовать
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if component == 0 {
let str = myPickerData[row]
if str == "Brown" {
myPickerData2 = ["Undergraduate"]
}
else if str == "Dartmouth" {
myPickerData2 = ["Undergraduate", "MBA"]
}
else {
myPickerData2 = ["Undergraduate", "JD", "MBA"]
}
pickerView.reloadComponent(1)
}
else {
uniSelected = myPickerData[pickerView.selectedRow(inComponent: 0)]
collegeSelected = myPickerData2[pickerView.selectedRow(inComponent: 1)]
}
}
//
Может содержать дубликаты, но вы можете попробовать
let mainDic:[String:[String]] = ["Select Your Dream College":[],
"Brown":["Undergraduate"],
"Dartmouth":["Undergraduate", "MBA"],
"Columbia": ["Undergraduate", "JD", "MBA"],
"Harvard",["Undergraduate", "JD", "MBA"]]
Тогда
myPickerData2 = mainDic[myPickerData[row]]!
pickerView.reloadComponent(1)
И использовать это в компоненте 0
myPickerData = Array(mainDic.keys)
Сделайте ваш источник данных равным mainDic
и работайте с ключами как myPickerData
, а текущее значение выбранного ключа как myPickerData2
Редактировать: В viewDidLoad
myPickerData = Array(mainDic.keys)
myPickerData2 = mainDic["Select Your Dream College"]! // it's default or simply []