Swift不符合协议
问题描述:
将使用Swift 6构建的应用程序拉到现在使用6测试版的系统后,我得到一个“EventFormViewController不符合协议UIPickerViewDataSource”。我已经挣扎了好几天了,有什么建议吗?Swift不符合协议
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.
}
*/
答
有很多的问题,在你的代码。
的函数头被注释。
// returns the number of 'columns' to display. func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return eventChoices.count }
应该是这样:
您的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
你有其封闭类闭括号。所以你的其他方法不在UIPickerViewDelegate的类中,因此委托人认为这些方法不存在。
因此,将括号移至代码的末尾以关闭该类。
其次,在didReceiveMemoryWarning
方法后出现错误。你有outcommented的方法标题:
// 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.
}
你能否解释一下吗? Swift 6是什么意思?你为什么使用beta 6?你有没有试过确保你实现了协议所需的所有方法? – 2015-02-08 22:56:45
只需阅读报告导航器中的生成日志。编译器会告诉你确切的丢失了哪个协议方法。 – 2015-02-08 23:00:08
你使用完全相同的代码吗? – 2015-02-08 23:06:03