0

I'm trying to set up an egg timer, and display a countdown when the button is pressed. If "Soft" is pressed, the countdown begins at 300 seconds and so on.

However, this message keeps coming up "cannot convert value of type String to expected argument type String". What should I do?

Egg Timer code

and here is the code:

import UIKit

class ViewController: UIViewController {

    let eggTimer = ["Soft" : 300, "Medium" : 420, " Hard" : 720]
    var secondsRemaining = 60

    @IBAction func hardnessPressed(_ sender: UIButton) {

        let hardness = [sender.currentTitle!]

        secondsRemaining = eggTimer[hardness]!
        Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(updateTimer), userInfo: nil, repeats: true)
    }

    @objc func updateTimer() {
        if secondsRemaining > 0 {
            print(" \(secondsRemaining) second")
            secondsRemaining -= 1
        }
    }
}
Andrew
  • 26,706
  • 9
  • 85
  • 101

2 Answers2

0

The line:

let hardness = [sender.currentTitle!]

is wrong, you are mixing up Objective-C method call syntax [object method...] with property syntax object.property.

What Swift sees is an expression sender.currentTitle!, which accesses an Objective-C property, inside an array literal [ ... ], and so creates an array of 1 element – hardness has type [String], array of String, and not String as you expected.

Remove the [ & ]. HTH

CRD
  • 52,522
  • 5
  • 70
  • 86
0

Error screenshot 1 -

enter image description here

Fix screenshot 2

just adding @objc line number 21.

enter image description here

TrickyJ
  • 61
  • 2