iosswiftuipickerviewdatasource

Error: Type 'GearViewController' does not conform to protocol 'UIPickerViewDataSource'


Error:

Type 'GearViewController' does not conform to protocol 'UIPickerViewDataSource'

Based on apple documentation there are only 2 required methods for a UIPickerViewDataSource. Both are included in the code below. I think the syntax is correct. (but probably not)

Class/control declaration, and init. (lots of other code removed for clarity. Full code available if needed, i'll edit. Just trying to stay brief.)

class  GearViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UIPickerViewDataSource, UIPickerViewDelegate{

@IBOutlet weak var pickerGearCategory: UIPickerView!

override func viewDidLoad() {

        super.viewDidLoad()

        pickerGearCategory.dataSource = self
        pickerGearCategory.delegate = self

    }

Delegate and datasources

  let gearCategoryPickerData = CategoryRepository.allCategories()
    //MARK: CategoryPicker- Delegates and data sources
    //MARK: CategoryPicker - Data Sources


    //Required
    func numberOfComponents(in pickerGearCategory: UIPickerView) -> Int {
        return 1
    }

    //Required
    func pickerGearCategory(pickerGearCategory: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return gearCategoryPickerData.count
    }


    func pickerGearCategory(pickerGearCategory: UIPickerView,titleForRow row: Int, forComponent component: Int) -> String? {
        return gearCategoryPickerData[row].name
    }

Solution

  • One of the protocol method that you've implemented is wrong, the method should be:

    func pickerView(UIPickerView, numberOfRowsInComponent: Int) {
    //Your implementation here
    }
    

    You've implemented the method as,

    func pickerGearCategory(pickerGearCategory: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    //Your implementation
    }
    

    Correct it and you should be good to go.