[英]Does not conform to protocol UIPickerViewDataSource
I don't know what's wrong with my code. 我不知道我的代码有什么问题。 I tried to follow the tutorial but same error happen.
我尝试按照本教程进行操作,但发生相同的错误。
Error: 错误:
Type 'FourthViewController' does not conform to protocol 'UIPickerViewDataSource'
类型“ FourthViewController”不符合协议“ UIPickerViewDataSource”
Here is my code: 这是我的代码:
import UIKit
let characters = ["Jaja Bink", "Luke", "Han Solo", "Princess Leia"];
let weapons = ["LightSaber", "Pistol", "Keris"];
class FourthViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
@IBOutlet weak var doublePicker: UIPickerView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 2
}
func pickerView(pickerView: UIPickerView,
titleForRow row: Int,
forComponent component: Int) -> String? {
if component == 0 {
return characters[row]
} else {
return weapons[row]
}
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int? {
if component == 0 {
return characters.count
} else {
return weapons.count
}
}
}
Replacing deprecated version of the method of protocol UIPickerViewDataSource
, if you're using Swift 3
could works for you. 如果您使用的是
Swift 3
,请替换协议UIPickerViewDataSource
的方法的不建议使用的版本。
Deprecated Method of Protocol 不建议使用的协议方法
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
Latest Method of Protocol in Swift 3 Swift 3中的最新协议方法
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
Note: Same true for other required protocol methods. 注意:其他必需的协议方法也是如此。 ie
即
Deprecated:
不推荐使用:
func pickerView(pickerView: UIPickerView!, numberOfRowsInComponent component: Int) -> Int
Recent Version:
最新版本:
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
Because your required delegate method is not correct. 因为您要求的委托方法不正确。
Replace it with this: 替换为:
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if component == 0 {
return characters.count
} else {
return weapons.count
}
}
Your delegate method returns Int?
您的委托方法返回
Int?
which is not correct. 这是不正确的。
You have to implement it's function: 您必须实现它的功能:
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.