[英]Type “myViewController” does not conform to protocol UIPIckerDataSource in Swift
I just create a new class in Swift, it's called myViewController
and it's a UIViewController
. 我只是在Swift中创建一个新类,它叫做myViewController
,它是一个UIViewController
。 Now I'm trying to make it a UIPickerViewDelegate and DataSource but i got a strange error 现在我试图让它成为UIPickerViewDelegate和DataSource,但我得到了一个奇怪的错误
import UIKit
class myViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
...
}
It says Type "myViewController" does not conform to protocol UIPIckerDataSource
over the UIPickerViewDataSource. 它说Type "myViewController" does not conform to protocol UIPIckerDataSource
UIPickerViewDataSource Type "myViewController" does not conform to protocol UIPIckerDataSource
。
Is it a bug of Xcode-Beta 3?? 这是Xcode-Beta 3的错误吗?
You need to implement all the required methods of UIPickerViewDataSource
and UIPickerViewDelegate
, if you want to conform to these protocols. 如果要符合这些协议,则需要实现UIPickerViewDataSource
和UIPickerViewDelegate
所需的所有方法。
Swift is more like java when it comes to protocols because if you don't implement all the required methods declared by a protocol you are going to get a compile time error instead of a run time exception. 当谈到协议时,Swift更像java,因为如果你没有实现协议声明的所有必需方法,你将得到编译时错误而不是运行时异常。
Fix-it in XCode 8.1 inserts a deprecated version of the method below if you're using Swift 3: 如果您正在使用Swift 3,则在XCode 8.1中修复它会插入以下方法的弃用版本:
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return componentNumber
}
In XCode 10.0+ 在XCode 10.0+中
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return componentNumber
}
Implement required method of UIPickerDataSource
as in documentation. 在文档中实现UIPickerDataSource
所需的方法。
The data source provides the picker view with the number of components, and the number of rows in each component, for displaying the picker view data. 数据源为选择器视图提供组件数量和每个组件中的行数,以显示选取器视图数据。 Both methods in this protocol are required. 该协议中的两种方法都是必需的。
So you need to implement these methods 所以你需要实现这些方法
func numberOfComponentsInPickerView(_ pickerView: UIPickerView!) -> Int {}
func pickerView(_ pickerView: UIPickerView!,
numberOfRowsInComponent component: Int) -> Int{}
My problem is protocol's method name is illegal, 我的问题是协议的方法名称是非法的,
@protocol ContactsSelectDelegate <NSObject>
- (void)selectContacts:(NSMutableArray *)contacts Tags:(NSMutableArray *)tags;
@end
Here, Tags:
should be tags:
. 在这里, Tags:
应该是tags:
。
I hope my answer is helpful. 我希望我的回答很有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.