jpsstack
jpsstack

Reputation: 1291

PickerView: Type 'ViewController' does not conform to protocol 'UIPickerViewDataSource'

I am using code from a sample project. It does work in the sample project, but not in my project.

SettingsViewController.swift:

import UIKit

class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
    let days:[String] = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

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

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

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

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


}

I am using Xcode 8.2.1, Swift 2.3

Upvotes: 1

Views: 638

Answers (1)

Ryan H.
Ryan H.

Reputation: 2593

The method signature for numberOfComponents(in:) changed from iOS 9.3 to iOS 10. Since you are targeting a legacy version of Swift, replace your current implementation of numberOfComponents(in:) with the appropriate version below.

Swift 2.3

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

Swift 3

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

The API changes can be seen here: https://developer.apple.com/library/content/releasenotes/General/iOS10APIDiffs/Swift/UIKit.html

Modified UIPickerViewDataSource

Declaration

From

protocol UIPickerViewDataSource : NSObjectProtocol {
    func numberOfComponentsInPickerView(_ pickerView: UIPickerView) -> Int
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
}

To

protocol UIPickerViewDataSource : NSObjectProtocol {
    func numberOfComponents(in pickerView: UIPickerView) -> Int
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
}

Upvotes: 2

Related Questions