Xcode 8 / Swift 3 : Simple UIPicker code not working
UIPickerViewDataSource
method numberOfComponentsInPickerView
is changed in Swift 3 like this that is the reason you are getting this error.
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return muteForPickerData.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return muteForPickerData[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
}
For more detail read Apple Documentation on UIPickerView
.
Note: You need to also add _
as first parameter label same like other methods in your UIPickerViewDelegate
method that is titleForRow
and didSelectRow
.
UIPickerView on Swift 3
Use this method, see the use of _
before pickerview. That is the only problem
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
}
PickerView: Type 'ViewController' does not conform to protocol 'UIPickerViewDataSource'
The method signature for numberOfComponents(in:)
changed from iOS 9.3 to iOS 10. Since you are targeting a legacy version of Swift, replace your current implementation of numberOfComponents(in:)
with the appropriate version below.
Swift 2.3
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
Swift 3
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
The API changes can be seen here: https://developer.apple.com/library/content/releasenotes/General/iOS10APIDiffs/Swift/UIKit.html
Modified UIPickerViewDataSource
Declaration
From
protocol UIPickerViewDataSource : NSObjectProtocol {
func numberOfComponentsInPickerView(_ pickerView: UIPickerView) -> Int
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
}
To
protocol UIPickerViewDataSource : NSObjectProtocol {
func numberOfComponents(in pickerView: UIPickerView) -> Int
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
}
iOS - Error Type 'ViewController' does not conform to protocol 'UIPickerViewDataSource'
For Swift 3, use these functions:
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
}
Swift/iOS : UIPicker display is not refreshed until tapped after changing the datasource programmatically
Figured it out when adding the asynchronous tag to the question... good old rubber duck debugging.
As said by @rmaddy, all UI updates must be done on the main thread.
In this case, replace
dossiers_picker.reloadAllComponents()
with
DispatchQueue.main.async {
self.dossiers_picker.reloadAllComponents()
}
UIPicker selectRow not working - what's wrong with this code?
It works if I put it in this method:
-(void) viewWillAppear: (BOOL) animated
Swift Picker Items
You should use this:
func pickerView(
_ pickerView: UIPickerView,
titleForRow row: Int,
forComponent component: Int
) -> String? {
pickerData[row]
}
instead of:
func pickerView(
pickerView: UIPickerView,
titleForRow row: Int,
forComponent component: Int
) -> String! {
pickerData[row]
}
The diff is in Omitting Argument Labels
.
Here is a doc link
Swift 2.1 - UIPickerView doesn't work well
Assuming you have set your delegate and datasource correctly in storyboard, your issue is incorrect method signature. Notice the spelling mistakes in your method signature (pickeView: UIPickerView, numberOfRowsInCompoment component: Int)
. Missing r
in your pickeView
and usage of m
instead of n
in your numberOfRowsInCompoment
.
Try with below function and you should be all set...
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) ->Int{
return 5
}
Related Topics
Forced to Cast, Even If Protocol Requires Given Type
Why Are Emoji Characters Like 👩👩👧👦 Treated So Strangely in Swift Strings
How to Set Layer Cornerradius For Only Bottom-Left, Bottom-Right, and Top-Left Corner
Getting Keyboard Size from Userinfo in Swift
How to Compare Two Dictionaries in Swift
How to Create Array of Unique Object List in Swift
Nsuserdefaults Not Working on Xcode Beta With Watch Os2
Binary Operator * Cannot Be Applied to Operands of Type Int and Double
Check Password String Strength Criteria in Swift
Choosing Coredata Entities from Form Picker
Swift: How to Use Preprocessor Flags (Like '#If Debug') to Implement API Keys
How to Set Up Scenekit Collision Detection
Difference Between Flatmap and Compactmap in Swift
Segue from Modal View to Tab Bar View Controller and Not Lose Tab Bar
How to Easily Duplicate/Copy an Existing Realm Object