0

I'm having some difficulty getting one of my view controllers to recognize a text input. I'm getting the "Cannot convert value of type 'String' to expected argument type 'Int'" error. I've seen some questions on stack overflow regarding the String/Int conversion but none that seemed to fix this specific situation.

The user is expected to input a number into the text field to set the value of pointsNeededText. The parameters of class Goal, however, state that the variable is an Int. How can I have the user type in a number into the text field and have it recognized as an Int? I set the keyboard type for that specific text field to be the number pad. Is this even the best way to do this? Should it be set up as something other than a text field? I'm a rookie at this, so any and all help would be greatly appreciated! Thank you!

import UIKit

class AddGoalsTableViewController: UITableViewController {

    var goal:Goal?

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "SaveGoal" {
            goal = Goal(goalText: nameOfRewardText.text!, pointsToCompleteGoal: pointsNeededText.text!, pointsEarnedTowardsGoal: goalProgressText.text!)
        }
    }

    @IBOutlet var goalTableTitleText : UILabel!
    @IBOutlet weak var goalProgressText: UILabel!
    @IBOutlet weak var nameOfRewardText: UITextField!
    @IBOutlet weak var pointsNeededText: UITextField!
    @IBOutlet weak var repeatSwitch: UISwitch!


override func viewDidLoad() {
    super.viewDidLoad()

Pic of UI

Bob F.
  • 93
  • 1
  • 11

1 Answers1

1

If the initializer for the Goal class needs the values as Int, you need to explicitly cast it from String to Int for it to work.

let integerValue = Int(stringValue)

Note that this generates an optional value, and thus, has to be unwrapped if your Goal init doesn't accept optionals.

Gabe
  • 206
  • 1
  • 5