简体   繁体   English

类型“myViewController”不符合Swift中的协议UIPIckerDataSource

[英]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. 如果要符合这些协议,则需要实现UIPickerViewDataSourceUIPickerViewDelegate所需的所有方法。

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 was that I had override in front of the implementation function, where Swift doesn't consider protocol method implementations to be overrides ( same ). 我的问题是我在实现函数前面有override ,其中Swift不认为协议方法实现是覆盖( 相同 )。 Just taking out the override keyword fixed the issue. 只需取出override关键字即可解决问题。

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.

相关问题 类型MyViewController不符合协议&#39;STPPaymentContextDelegate&#39; - Type MyViewController does not conform to protocol 'STPPaymentContextDelegate' iOS:&#39;MyViewController&#39;不符合协议&#39;UITableViewDataSource&#39; - iOS : 'MyViewController' does not conform to protocol 'UITableViewDataSource' 类型不符合协议Swift - Type does not conform to protocol Swift Swift - 类型“*”不符合协议“*” - Swift - Type '*' does not conform to protocol '*' 类型不符合协议序列类型 - Swift - type does not conform to protocol Sequence Type - Swift 类型“NSPersistentStore”在swift中不符合协议“BooleanType” - Type 'NSPersistentStore' does not conform to protocol 'BooleanType' in swift Swift:类型&#39;ViewController&#39;不符合协议&#39;UIPageViewControllerDataSource&#39; - Swift: Type 'ViewController' does not conform to protocol 'UIPageViewControllerDataSource' Swift 2.0类型&#39;()&#39;不符合协议 - Swift 2.0 Type '()' does not conform to protocol Swift:`类型&#39;[String]&#39;不符合协议&#39;StringLiteralConvertible&#39; - Swift: `Type '[String]' does not conform to protocol 'StringLiteralConvertible'` Swift-类型&#39;CircularTransition&#39;不符合协议&#39;UIViewControllerAnimatedTransitioning&#39; - Swift - Type 'CircularTransition' does not conform to protocol 'UIViewControllerAnimatedTransitioning'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM