-1
import UIKit

class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
    @IBOutlet weak var picker: UIPickerView!

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

    override func viewDidLoad() {
        super.viewDidLoad()

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

        // Input data into the Array:
        pickerData = ["Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "Item 6"]
    }

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

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return pickerData.count
    }

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

I keep getting the message "type 'ViewController' does not conform to protocol 'UIPickerViewDataSource'" in Swift 8.2.1 and I don't know how to solve the problem.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Press ⌘4 (Issue Navigator), click on the disclosure triangle next to the error and implement the displayed missing method(s) – vadian Mar 24 '17 at 20:45
  • I get a circle next to it and there is no implementation shown to fix it – Alhanouf Al-Marjan Mar 24 '17 at 20:55
  • Not in the editor window, in the Issue navigator (left side bar). Press ⌘4 or click on the *caution* icon. Or ⌘-click on UIPickerViewDataSource to get the declaration. – vadian Mar 24 '17 at 20:58
  • Providing Counts for the Picker View func numberOfComponents(in: UIPickerView) Required. Called by the picker view when it needs the number of components. func pickerView(UIPickerView, numberOfRowsInComponent: Int) Required. Called by the picker view when it needs the number of rows for a specified component. Relationships. This also does not solve the problem – Alhanouf Al-Marjan Mar 24 '17 at 21:39
  • Do you see the difference between your `numberOfComponentsInPickerView` method and the displayed one ? Just change the syntax like suggested in the answer. And add the underscore character in the `titleForRow` method. Another way to figure out the proper syntax is to comment out the method, retype the first characters and use code completion. – vadian Mar 24 '17 at 21:45

1 Answers1

0

You're missing a required function - actually, you have it, but it's not-quite-right...

func numberOfComponents(in pickerView: UIPickerView) -> Int

In Xcode, if you right-click on UIPickerViewDataSource and select Jump to Definition you will see the required and optional protocol functions.

DonMag
  • 69,424
  • 5
  • 50
  • 86
  • I actually get another error message with this line // The number of rows of data func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { return pickerData.count } but it can be fixed by putting _ as i get in the error message, but still i didn't get how to fix it – Alhanouf Al-Marjan Mar 24 '17 at 20:50
  • Here is an explanation of the "_" in the function definition... http://stackoverflow.com/questions/39627106/why-do-i-need-underscores-in-swift – DonMag Mar 24 '17 at 21:04