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

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

错误:

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

基于apple documentation,UIPickerViewDataSource 只需要 2 个方法。两者都包含在下面的代码中。我认为语法是正确的。 (但可能不是)

Class/control 声明和初始化。 (为清楚起见,删除了许多其他代码。如果需要,可以提供完整代码,我将进行编辑。只是尽量保持简短。)

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

@IBOutlet weak var pickerGearCategory: UIPickerView!

override func viewDidLoad() {

        super.viewDidLoad()

        pickerGearCategory.dataSource = self
        pickerGearCategory.delegate = self

    }

委托和数据源

  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
    }

numberOfComponents 在那里,但是 pickerView 在哪里?

你实现的其中一个协议方法是错误的,方法应该是:

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

您已将方法实现为,

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

更正它,你应该可以开始了。

只需添加 func pickerView(UIPickerView, numberOfRowsInComponent: Int) 函数