I’m a complete newbie and started learning Swift through an online course last week.
My problem is, that I somehow receive the same error in different projects. Every time I run the Simulator and click on one of my linked buttons, nothing happens and the Xcode gives me the following error message: "unrecognized selector send to instance". Can someone help me? 🙂
I followed the course very closely and got the same code as the person in video, also I started from scratch and neither did work.
class ViewController: UIViewController {
let eggTimes = ["Soft": 300, "Medium": 420, "Hard": 720]
var secondsRemaining = 60
@IBAction func hardnessSelected(_ sender: UIButton) {
let hardness = sender.currentTitle!
secondsRemaining = eggTimes[hardness]!
Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(updateTimer), userInfo: nil, repeats: true)
}
@objc func updateTimer() {
if secondsRemaining > 0 {
print("(secondsRemaining) seconds.")
secondsRemaining -= 1
}
}
}
2
Answers
First go to Storyboard, find that button and right click on it, check all of it’s IBActions and make sure you only have one, called
hardnessSelected
.If this does not help,
Try deleting
_ sender: UIButton
parameter inhardnessSelected
IBAction.You’ve likely connected the wrong thing to
hardnessSelected
in Interface Builder (the Storyboard). Make sure it’s actually a UIButton. Read the entire error message; it likely tells you the types involved.