UIPickerView Delegate方法返回nil

时间:2017-06-06 14:34:29

标签: ios swift uipickerview

我正在使用UIPickerView实例来确定项目中的某些设置。

在我的viewDidLoad()中,我初始化了一个包含我所有数据的数组:

pickerData = ["5 Minutes", "6 Minutes","7 Minutes","8 Minutes","9 Minutes","10 Minutes","11 Minutes","12 Minutes","13 Minutes","14 Minutes","15 Minutes","16 Minutes","17 Minutes","18 Minutes","19 Minutes","20 Minutes","21 Minutes","22 Minutes","23 Minutes","24 Minutes","25 Minutes","26 Minutes","27 Minutes","28 Minutes","29 Minutes","30 Minutes","31 Minutes","32 Minutes","33 Minutes","34 Minutes","35 Minutes","36 Minutes","37 Minutes","38 Minutes","39 Minutes","40 Minutes","41 Minutes","42 Minutes","43 Minutes","44 Minutes","45 Minutes","46 Minutes","47 Minutes","48 Minutes","49 Minutes","50 Minutes","51 Minutes","52 Minutes","53 Minutes","54 Minutes","55 Minutes","56 Minutes","57 Minutes","58 Minutes","59 Minutes","60 Minutes",]

在UIPickerView委托方法中,我需要在某个索引处检索一个元素,然后将其转换为整数。

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {


        let minuteIndex = Int(pickerData[row])
        print(minuteIndex)
    }

当我开始在UIPickView中进行选择时, Xcode控制台将每个选项打印为零。 enter image description here

我很困惑有什么不对?如何解决这个问题,提前谢谢。

2 个答案:

答案 0 :(得分:1)

您正在尝试将一个不是纯Int的String转换为Int。 尝试从您的值中删除“分钟”部分,看看它是否有效。

编辑:施放不转换。

答案 1 :(得分:1)

您可以使用row + 5,忽略字符串/内容。

let minuteIndex = row + 5

没有演员或转换。