Swift数组 - 如果值不存在

时间:2016-05-02 05:41:52

标签: arrays swift

我有一个UIPickerView将数据加载到一个数组 - 它正在工作,但是 然后我在另一个页面上有相同的UIPickerView,我想显示在选择项目时保存的数据。如果数组中存在数据,则此方法有效。如果数据不存在,则会发生索引超出范围错误,并且应用程序崩溃。

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

我需要的是,如何在不破坏应用程序的情况下测试阵列中的值是否不存在(尚未在上一页中输入)。

1 个答案:

答案 0 :(得分:0)

您可以检查index是否大于或等于数据数组中的项目数。

if(index < data.count){
    // When data exists
} else{
    // When data does not exist
}
相关问题