Type "myViewController" does not conform to protocol UIPIckerDataSource in Swift
Asked Answered
C

5

39

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

import UIKit

class myViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
   ...
}

It says Type "myViewController" does not conform to protocol UIPIckerDataSource over the UIPickerViewDataSource.

Is it a bug of Xcode-Beta 3??

Screenshot for compile-time error

Conventional answered 26/7, 2014 at 12:11 Comment(1)
1) Please show how you implemented the required UIPickerViewDataSource methods. - 2) Why is there a typo in "UIPIckerDataSource"? Did you copy/paste the error message? - 3) The current version is Xcode 6 beta 4.Echinus
P
55

You need to implement all the required methods of UIPickerViewDataSource and UIPickerViewDelegate, if you want to conform to these protocols.

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.

Palais answered 26/7, 2014 at 12:27 Comment(4)
Getting the same red warning for UIPickerViewDataSource. The UIPickerViewDelegate is fine. As soon as I add UIPickerViewDataSource I get the error. Strange.Fluorescence
I was facing the same exact error (running Xcode 6.1) To "fix it" (because it makes 0 sense why it now works), I simply removed the UIPickerViewDataSource protocol declaration. Then set self as the dataSource, then implemented the required 2 delegate methods. Finally re-added the protocol declaration. Now it works!Extrasensory
The "coolest" company in the world really needs to improve their half-coocked IDE...Too many bugs and weird errors for a company that "focus on excellency"Deadly
The second paragraph of your answer really saved my life! Thanks!Tronna
C
15

Fix-it in XCode 8.1 inserts a deprecated version of the method below if you're using Swift 3:

func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
    return componentNumber
}

In XCode 10.0+

func numberOfComponents(in pickerView: UIPickerView) -> Int {
    return componentNumber
}
Confirmed answered 7/12, 2016 at 0:4 Comment(0)
L
11

Implement required method of UIPickerDataSource as in documentation.

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{}
Linter answered 26/7, 2014 at 12:33 Comment(0)
V
0

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). Just taking out the override keyword fixed the issue.

Vandusen answered 28/7, 2015 at 21:17 Comment(0)
W
0

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.

Waterside answered 26/4, 2016 at 17:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.