PickerView:类型'ViewController'不符合协议'UIPickerViewDataSource'

时间:2017-01-04 15:15:49

标签: swift3 xcode8

我正在使用示例项目中的代码。它在示例项目中有效,但在我的项目中没有。

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.
    }


}

我正在使用Xcode 8.2.1,Swift 2.3

1 个答案:

答案 0 :(得分:2)

es5的方法签名已从iOS 9.3更改为iOS 10.由于您的目标是Swift的旧版本,请使用下面的相应版本替换您当前的numberOfComponents(in:)实现。

Swift 2.3

numberOfComponents(in:)

Swift 3

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

可以在此处查看API更改:https://developer.apple.com/library/content/releasenotes/General/iOS10APIDiffs/Swift/UIKit.html

已修改 UIPickerViewDataSource

声明

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

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