Using UIPickerView with multiple components in swift

ⅰ亾dé卋堺 提交于 2019-12-05 11:39:16

You need to tell Swift that your wheelContents array is an array of array of String:

var wheelContents:[[String]] = []

If you don't explicitly give wheelContents a type, Swift infers it to be NSArray which is what is giving you problems.

You need to say swift, that your wheelContents is an array of String arrays:

var wheelContents:[[String]] = []

Also you should set the delegate of your pickerView to self because you handle the delegate in your class. Otherwise the functions won't work properly:

//In the viewDidLoad method
bigPicker.delegate = self

Since you declared wheelContents like this wheelContents = [], without specifying its elements type, the compiler automatically infers that it is array of AnyObjects aka [AnyObject].

That's the reason why when you are returning wheelContents[component].count it generates an error: at that moment the compiler is expecting a String! but you are providing an AnyObject.

It's a really easy fix, you should just specify what the content of the array it is going to be when you declare it. (it's an array of arrays of strings aka [[String]])

You need to set the datasource of pickerView along with the delegate in viewDidLoad

pickerView.dataSource = self
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!