unrecognized selector sent to instance 0x144b071

I am trying to learn Xcode/Swift and have a problem.
I get the following error after build and after I have entered my age. Usually its a typo but this is not the case.
Here is the error:
Code Block [CalorieCounter.FirstViewController yourAge:]: unrecognized selector sent to instance 0x144b071

Here is the whole code:
Code Block
//
//  FirstViewController.swift
//  CalorieCounter
//
//  Created by Tony Hudson on 11/04/2021.
//
import UIKit
var userAge: Double?
var userWeight: Double?
var userGender: String = "Female"
class FirstViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
    @IBOutlet weak var yourAge: UITextField!
    @IBOutlet weak var yourWeight: UITextField!
    @IBOutlet weak var segmentedControl: UISegmentedControl!
    @IBAction func genderButton(_ sender: UISegmentedControl) {
        switch sender.selectedSegmentIndex {
        case 0:
            userGender = "Female"
        case 1:
            userGender = "Male"
        default:
            return
        }
    }
    @IBAction func savePersonalInfo(_ sender: Any) {
        userWeight = Double(yourWeight.text!)
        userAge = Double(yourAge.text!)
    }
}

Can anyone help me solve this problem


Replies

We do not see the error, but I understand that's the thread title…

Most usual cause is that one of the IBOutlets are not connected to their object in storyboard.
=> Check all connections (specially yourAge)
=> if appears OK, do an option-clean build folder

If not enough, remove the connection of yourAge (in Storyboard, right click on the textField and click the small x in the "Referenced Outlet section".
And reconnect.

Another possible cause depending on the error you get:
Code Block
@IBAction func savePersonalInfo(_ sender: Any) {
userWeight = Double(yourWeight.text!)
userAge = Double(yourAge.text!)
}

It is risky to force unwrap. yourWeight.text may be nil. And you get an optional, not a Double.
And sender is a button ? It is better to have UIButton and not Any as argument. To change, you have to disconnect the IBAction first, then change Any to UIButton then reconnect IBAction.
Are you sure age should be a Double, not an Int ?

More robust code:
Code Block
@IBAction func savePersonalInfo(_ sender: UIButton) {
userWeight = Double(yourWeight.text ?? "0.0") ?? 0.0
userAge = Double(yourAge.text ?? "0.0") ?? 0.0
}


Here is the error:

I'm not sure what's happening, but the error message is hidden and we cannot see it.
I needed to explore the HTML source for it:
Code Block
[CalorieCounter.FirstViewController yourAge:]: unrecognized selector sent to instance 0x144b071


This is very odd, as the selector yourAge: contains a colon.
It seems as if iOS is invoking some action method (yourAge(_:) in Swift) linked to some UI component (maybe a UITextField).

You once connected Action from the UITextField mistakenly?
I guess you removed that mistakenly created action method from code, but you have not removed the connection in the Interface Builder.
Open the Connections inspector, then find and remove the action connection showing yourAge: (grouped in Sent Evens).

I have tried all of the above suggestions and I still get the same error.
Here is the code after modification:
Code Block //
//  FirstViewController.swift
//  CalorieCounter
//
//  Created by Tony Hudson on 11/04/2021.
//
import UIKit
var userAge: Double?
var userWeight: Double?
var userGender: String = "Female"
class FirstViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
    @IBOutlet weak var yourAge: UITextField!
    @IBOutlet weak var yourWeight: UITextField!
    @IBOutlet weak var segmentedControl: UISegmentedControl!
    @IBAction func genderButton(_ sender: UISegmentedControl) {
        switch sender.selectedSegmentIndex {
        case 0:
            userGender = "Female"
        case 1:
            userGender = "Male"
        default:
            return
        }
    }
    @IBAction func savePersonalInfo(_ sender: UIButton) {
        userWeight = Double(yourWeight.text ?? "0.0") ?? 0.0
        userAge = Double(yourAge.text ?? "0.0") ?? 0.0
    }
}

Has anyone got ant other ideas?

Where exactly do you get the crash ?
When you do what ?

have you rebuilt the connections for yourAge textField from IB to the IBOutlet ?

Why don't you define the var inside the class ?

Code Block
class FirstViewController: UIViewController {
var userAge: Double?
var userWeight: Double?
var userGender: String = "Female"