Bizarre: Setting picker dataSource not needed ?

I have created a picker programmatically in code, in a view controller.


In viewDidLoad, I set

        picker!.delegate = self
        picker!.dataSource = self

And creates all functions to conform to UIPickerViewDataSource, UIPickerViewDelegate


Works perfectly. Picker displays, with the content defined in an array ; allows for selection…


So, for testing, I commented out picker!.dataSource = self


I thought that would not work anymore.

Surprise, it still works the same !


I tested with the log:

        print(picker!.delegate)
        print(picker!.dataSource)

and got

Optional(<simpleTest.SelectCellFromItem2ViewController: 0x7f928982ee00>)

nil


So, dataSource is nil.


How can this work ?

Does it mean that when no dataSource is defined, delegate is used by default ?

Accepted Reply

Filed a DTS and got the following explanation:


This is as a result of iOS moving away from informal to formal protocol definitions.

The runtime checks if you conform and implement those methods and they get called.


You should definitely set the picker view’s dataSource to your view controller.

If not, when these are called:


func numberOfComponents(in pickerView: UIPickerView) -> Int

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int


… “pickerView” will be nil. Often times you want that to be valid value so to be sure you are targeting the right picker view (in the case of multiple picker views at once).


I consider this a bug in that you “should” set the dataSource. Please file a bug report to "http://bugreporter.apple.com”.


Filed bug report 42512684

Replies

Filed a DTS and got the following explanation:


This is as a result of iOS moving away from informal to formal protocol definitions.

The runtime checks if you conform and implement those methods and they get called.


You should definitely set the picker view’s dataSource to your view controller.

If not, when these are called:


func numberOfComponents(in pickerView: UIPickerView) -> Int

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int


… “pickerView” will be nil. Often times you want that to be valid value so to be sure you are targeting the right picker view (in the case of multiple picker views at once).


I consider this a bug in that you “should” set the dataSource. Please file a bug report to "http://bugreporter.apple.com”.


Filed bug report 42512684