1

I know there's many questions for this issue out there, but I still cannot get my specific code to work. Sorry if this is noob question. This is literally my entire code below. If I break inside viewDidLoad my delegate and dataSource sets to self run without issue. The exception happens immediately after this event before any of the implemented UIPickerViewDataSource functions have a change to run. I included the debug below.

class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
    var listingTypesList:[String:Int] = [
        "Bench": 0,
        "Squat": 1,
        "Dead": 2
    ]

    @IBOutlet weak var listingType: UIPickerView!

    override func viewDidLoad() {
        super.viewDidLoad()

        listingType.delegate = self
        listingType.dataSource = self
    }

    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return listingTypesList.count
    }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        var listingTypesArray = Array(listingTypesList.keys)
        return listingTypesArray[row]
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

2017-10-03 20:41:36.038930-0400 Lifting Trainer[328:19327] -[UIView numberOfComponentsInPickerView:]: unrecognized selector sent to instance 0x107811de0 2017-10-03 20:41:36.041055-0400 Lifting Trainer[328:19327] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView numberOfComponentsInPickerView:]: unrecognized selector sent to instance 0x107811de0' * First throw call stack: (0x181697d38 0x180bac528 0x1816a51f8 0x18ae5fcc4 0x18169d6e4 0x1815830dc 0x18acf93a8 0x18acf8504 0x18acf85e4 0x18aaa476c 0x18aaa445c 0x18aaa3918 0x18aaa3770 0x18aaaf39c 0x18aaae5c4 0x18aaab608 0x1a03c04fc 0x18ab1b068 0x18ad0a954 0x18ad0f6e4 0x18af9d454 0x18b26d1f0 0x18af9d0b8 0x18af9d928 0x18b7066e8 0x18b70658c 0x18b4829c0 0x18b617fc8 0x18b482870 0x18b26c850 0x18ad0de28 0x18b1116ec 0x183d39768 0x183d42070 0x105a6945c 0x105a75b74 0x183d6da04 0x183d6d6a8 0x183d6dc44 0x181640358 0x1816402d8 0x18163fb60 0x18163d738 0x18155e2d8 0x1833eff84 0x18ab0b880 0x1049993ac 0x18108256c) libc++abi.dylib: terminating with uncaught exception of type NSException

David Choi
  • 6,131
  • 10
  • 28
  • 28
  • There no issue in your code, it working perfectly can you attach the log please. So it easy to debug – Jaydeep Vyas Oct 03 '17 at 03:53
  • Can you please check whether the correct UIPickerView is linked to the listingType property. – Ruchira Randana Oct 03 '17 at 03:58
  • @RuchiraRandana i think there is no issue with connection as he says `If I break inside viewDidLoad my delegate and dataSource sets to self run without issue.` – Jaydeep Vyas Oct 03 '17 at 04:04
  • Some others have experienced similar issues: try this https://stackoverflow.com/a/38747910/968904 – JonEasy Oct 03 '17 at 04:24
  • unrelated but maybe helpful: [keys from a Swift dictionary are not guaranteed to be in any particular order](https://stackoverflow.com/questions/29601394/swift-stored-values-order-is-completely-changed-in-dictionary), so creating `listingTypesArray` each time `titleForRow` is called might result in unexpected behavior. Better to create `listingTypesArray` in your viewDidLoad method or an object property. – Michael Dautermann Oct 03 '17 at 06:26
  • @RuchiraRandana, when I select my UIPickerView and then go look in the Connections inspector I can see my listingType property. – David Choi Oct 04 '17 at 00:45
  • Found issue. I am using one of the udemy iOS tutorials and it had me control drag and drop, from the connection inspector, the delegate and datasource--onto the unpicked control. Once I removed those everything worked. Sorry for the trouble. Thanks. – David Choi Oct 04 '17 at 04:04

1 Answers1

0

I also had the same problem but I solved it using the code below:

override func viewDidLoad() {
    super.viewDidLoad()
    currencyPicker.delegate = self
    currencyPicker.dataSource = self
   
}
Edward Anthony
  • 3,354
  • 3
  • 25
  • 40
Annkit
  • 1