Search code examples
iosswiftuipickerview

UIPickerView Index as Int


I have a picker view. I am able to capture the String out of the picker view, but really I just want the index. I thought maybe the selectedRow(InComponent: Int) would work, but when I try this I get a crash saying

index 2 beyond bounds [0 .. 0]

This is what I am trying

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, InComponent component: Int) {    
    timeFrame = fixedAmountPickerView.selectedRow(inComponent: row) 
}

What can I do to get the Index as an Int?


Solution

  • You can get the index in different ways:

    1. In your didSelectRow the row parameter is your selected row
    2. You can type fixedAmountPickerView.selectedRow(inComponent: 0) if you´re outside the didSelectRow