0

I have a UIPickerView that loads data to an array - it's working, but then I have the same UIPickerView on another page that I want to display data that is saved when I select an item. This works, if the data exists in the array. If the data dose not exist an index out of range error occurs, and the app crashes.

    if data[indexRow].isEmpty
    {
        distanceLabel.text = ""//works
    }else if (){//need this code
        print("data")
    }else print("data")//need this code

What I need is, how can I test if the value dose not exists (has not been entered on the previous page) in the array without crashing the app.

1
  • if indexRow < data.count { // do something element exists at index
    – Leo Dabus
    Commented May 2, 2016 at 6:31

1 Answer 1

1

You could check if the index is greater than or equal to the number of items in your data array.

if(index < data.count){
    // When data exists
} else{
    // When data does not exist
}
3
  • Thanks but no still getting an Index out of range error Commented May 2, 2016 at 6:09
  • Yes sorry that is the solution. Thanks Commented May 2, 2016 at 6:20
  • Remember to accept this as the answer if it answered your question.
    – spencer.sm
    Commented May 2, 2016 at 6:30

Not the answer you're looking for? Browse other questions tagged or ask your own question.