Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

    import UIKit

class FourthViewController: UIViewController,UIPickerViewDelegate,UIPickerViewDataSource {


    @IBOutlet weak var picker: UIPickerView!

      var pickerData: [String] = [String]()


    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

        self.picker.delegate = self
        self.picker.dataSource = self

        pickerData = ["Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "Item 6"]    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    //MARK: - Delegates and data sources
    //MARK: Data Sources
    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
        return 1
    }
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return pickerData.count
    }

    //MARK: Delegates
    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return pickerData[row]
    }
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

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

Just for testing some features but i dont get the problem

like image 662
Fulachs Avatar asked Dec 03 '22 23:12

Fulachs


1 Answers

As you have found, you need to implement numberOfComponents(in:).

Your numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int, needs to be changed to:

func numberOfComponents(in pickerView: UIPickerView) -> Int {
    return 1
}

Also you need to modify your pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?:

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    return pickerData[row]
}

In Swift 3 many methods are renamed. Check the latest reference, and be careful about this. And you better remark Xcode version in your question.

like image 95
OOPer Avatar answered Feb 19 '23 05:02

OOPer