ios 类型“myViewController”不符合 Swift 中的协议 UIPIckerDataSource
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24970877/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Type "myViewController" does not conform to protocol UIPIckerDataSource in Swift
提问by r4id4
I just create a new class in Swift, it's called myViewController
and it's a UIViewController
.
Now I'm trying to make it a UIPickerViewDelegate and DataSource but i got a strange error
我只是在 Swift 中创建了一个新类,它被调用myViewController
并且它是一个UIViewController
. 现在我试图使它成为 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 上说。
Is it a bug of Xcode-Beta 3??
它是 Xcode-Beta 3 的错误吗??
回答by salman140
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 protocolsbecause 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,因为如果您没有实现协议声明的所有必需方法,您将获得编译时错误而不是运行时异常。
回答by offbyone
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 中的 Fix-it 会插入以下方法的弃用版本:
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return componentNumber
}
In XCode 10.0+
在 XCode 10.0+ 中
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return componentNumber
}
回答by codester
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{}
回答by amr
回答by DawnSong
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:
.
I hope my answer is helpful.
在这里,Tags:
应该是tags:
。
我希望我的回答有帮助。