Nan
Nan

Reputation: 524

UIPickerView Delegate Method return nil

I am using a UIPickerView instance to determine certain setting in my project.

In my viewDidLoad(), I initialise an array which includes all my data:

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",]

In UIPickerView delegate method, I need to retrieve the one element at certain index then convert it into an Integer.

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


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

When I start to make my selection in the UIPickView, Xcode console print every selection as nil. enter image description here

I am quite confuse what's wrong? How to resolve this, thanks in advance.

Upvotes: 0

Views: 149

Answers (2)

Mozahler
Mozahler

Reputation: 5303

You could use row + 5, ignore the string/contents.

let minuteIndex = row + 5

No casts or conversions.

Upvotes: 1

TawaNicolas
TawaNicolas

Reputation: 646

You're trying to cast* a String which is not a pure Int into an Int. Try removing the "Minutes" part from your values and see if it works.

Edit: cast not convert.

Upvotes: 1

Related Questions