Swift不符合协议

拉了一个使用Swift 6构build的应用程序到现在使用6 beta的系统后,我得到一个“EventFormViewController不符合协议UIPickerViewDataSource”。 我已经挣扎了好几天了,有什么build议吗?

import UIKit var eventChoices = [ ["5","10","15","30","45","60","90","120","150","180"], ["Hospital Committee","Peer Review","EHR Improvement","Quality Improvement","Business Development"], ] class EventFormViewController: UIViewController,UIPickerViewDataSource,UIPickerViewDelegate{ @IBOutlet weak var eventPicker: UIPickerView! @IBOutlet weak var eventLabel: UILabel! @IBOutlet weak var commentField: UITextField! func updateLabel(){ let selectedTime = eventChoices[0][eventPicker.selectedRowInComponent(0)] let event = eventChoices[1][eventPicker.selectedRowInComponent(1)] eventLabel.text = "Chose \(event) for \(selectedTime) mins" } func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { updateLabel() } override func viewDidLoad() { super.viewDidLoad() } // Do any additional setup after loading the view. } func didReceiveMemoryWarning() { didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } // returns the number of 'columns' to display. func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int { return eventChoices.count } // returns the # of rows in each component.. func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{ return eventChoices[component].count } func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! { return eventChoices[component][row] } func pickerView(pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat { if (component == 0) { return 50.0; } return 300.0; } /* // MARK: - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) { // Get the new view controller using segue.destinationViewController. // Pass the selected object to the new view controller. } */ 

你的代码中有很多问题。

1

函数标题被注释。

 // returns the number of 'columns' to display. func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int { return eventChoices.count } 

它应该是这样的:

 // returns the number of 'columns' to display. func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int { return eventChoices.count } 

2

你正在closures你的类定义在viewDidLoad实现之后。

它应该是这样的:

 import UIKit var eventChoices = [ ["5","10","15","30","45","60","90","120","150","180"], ["Hospital Committee","Peer Review","EHR Improvement","Quality Improvement","Business Development"], ] class EventFormViewController: UIViewController,UIPickerViewDataSource,UIPickerViewDelegate { @IBOutlet weak var eventPicker: UIPickerView! @IBOutlet weak var eventLabel: UILabel! @IBOutlet weak var commentField: UITextField! func updateLabel() { let selectedTime = eventChoices[0][eventPicker.selectedRowInComponent(0)] let event = eventChoices[1][eventPicker.selectedRowInComponent(1)] eventLabel.text = "Chose \(event) for \(selectedTime) mins" } func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { updateLabel() } override func viewDidLoad() { super.viewDidLoad() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } // returns the number of 'columns' to display. func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int { return eventChoices.count } // returns the # of rows in each component.. func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { return eventChoices[component].count } func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! { return eventChoices[component][row] } func pickerView(pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat { var width = 300.0 if (component == 0) { width = 50.0; } return width; } /* // MARK: - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) { // Get the new view controller using segue.destinationViewController. // Pass the selected object to the new view controller. } */ } 

你有一些打字错误:

在你的didReceiveMemoryWarning之前,你有一个closures括号,closures这个类。 所以你的其他方法不在UIPickerViewDelegate的类中,所以委托人认为这些方法不存在。

所以把括号移到你的代码的结尾来closures这个类。

其次,在didReceiveMemoryWarning方法之后你有一个错误。 你已经注释了一个方法头:

  // returns the number of 'columns' to display. func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int { return eventChoices.count } 

所以改变看起来像这样:

  // returns the number of 'columns' to display. func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int { return eventChoices.count } 

最后但并非最不重要的是,您需要重写didReceiveMemoryWarning方法。 所以改变一下:

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

对此:

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