Swift 3 - SIGABRT error - numberOfComponentsInPickerView:]: unrecognised selector sent to instance

久未见 提交于 2020-03-03 09:23:27

问题


Just updated to Swift 3 and xCode 8 Beta 4.

After fixing all the code issues I now have an error free project but it is throwing a SIGABRT error when clicking one particular button on my front screen.

I am sure that it is something to do with the UIPickerView element on the destination page as I have deleted, checked and re added all segues and outlets to make sure everything is clear from that side.

The console is showing the following:

2016-08-02 18:59:46.607 ForceIt![38735:2895259] -[ForceIt_.DirectoryViewController numberOfComponentsInPickerView:]: unrecognized selector sent to instance 0x7fcd68c0c210 2016-08-02 18:59:46.618 ForceIt![38735:2895259] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ForceIt_.DirectoryViewController numberOfComponentsInPickerView:]: unrecognized selector sent to instance 0x7fcd68c0c210'

The code from the relevant viewcontroller is as follows:

import UIKit

var forceSelectedForTabView = String()
var forceSelectedPositionInArray = Int()

class DirectoryViewController: UIViewController, UIPickerViewDelegate {


    @IBOutlet weak var forcePicker: UIPickerView!
    @IBOutlet weak var selectedContactLabel: UILabel!
    @IBOutlet weak var selectedPhoneTextView: UILabel!
    @IBOutlet weak var selectedWebsiteTextView: UILabel!

    //function for the number of columns in the picker
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }

    //function counting the array to give the number of rows in the picker
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return forcePickerData.count
    }

    //function displaying the array rows in the picker as a string
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return forcePickerData[row]

    }

    //function allowing the font and colour of the picker to be changed
    func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
        let titleData = forcePickerData[row]
        let myTitle = NSAttributedString(string: titleData, attributes: [NSFontAttributeName:UIFont(name: "Verdana", size: 25.0)!,NSForegroundColorAttributeName:UIColor.black])
        return myTitle
    }

    //function returning the selected row from the picker
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

        forceSelectedForTabView = String(forcePickerData[row])
        forceSelectedPositionInArray = forcePickerData.index(of: forcePickerData[row])!


        self.selectedContactLabel.text = issiData[Int(forcePickerData.index(of: forcePickerData[row])!)]


        self.selectedPhoneTextView.text = phoneData[Int(forcePickerData.index(of: forcePickerData[row])!)]


        self.selectedWebsiteTextView.text = websiteData[Int(forcePickerData.index(of: forcePickerData[row])!)]

    }
}

回答1:


numberOfComponentsInPickerView: is a method declared in UIPickerViewDataSource, but your DirectoryViewController lacks UIPickerViewDataSource in its conforming protocol list.

Change the class header as:

class DirectoryViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {

Without that, Swift cannot expose numberOfComponentsInPickerView: from the method numberOfComponents(in:).




回答2:


I have managed to fix this issue by removing all previous code and inserting the methods again from the code library. There was nothing different in the new code, but it seems to have been accepted.

All is well again - guess I might have just angered a bug during the update process.



来源:https://stackoverflow.com/questions/38728094/swift-3-sigabrt-error-numberofcomponentsinpickerview-unrecognised-selecto

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!