Search code examples
swiftuiswitchunrecognized-selector

How to retrieve the value of switch and pass into Object?


I am trying to get the value of a switch and pass it to an object in a view controller.

Below is the code of my view controller. I have created a label, switch outlet, and switch action.

@IBOutlet weak var firstTimeConducting: UILabel!
@IBOutlet weak var firstTime_Conduct: UISwitch!

@IBAction func firstTimeConductSwitchTap(_ sender: Any) {
    firstTimeConductTapped()
}

override func viewDidLoad() {
    super.viewDidLoad()
    firstTimeConductTapped()
}

func firstTimeConductTapped() {
    if firstTime_Conduct.isOn {
        value = "Yes"
    } else {
        value = "No"
    }
}

I have debugged it and it enters and gets the value as "yes" when I click on the switch from off to on state in simulator.

Then after returning to function firstTimeConductSwitchTap(), it crashes saying the following error

"Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[app.RequestFormViewController first_TimeConduct:]: unrecognized selector sent to instance 0x7fb900421fc0'

I am just retrieving the value of "value" variable and passing it to object to display.

Can you suggest whether I am following the correct procedure and why I get this error?


Solution

  • The error is pretty clear:

    In Interface Builder there is a dead connection to a selector first_TimeConduct. Most likely you renamed the action method.

    Search for first_TimeConduct with ⇧⌘F and delete the connection.