Value of type 'String?' has no member 'Int'

1

I have the following problem:

  

Value of type 'String?' has no member 'Int'

What is the reason for the error and what is the solution?

class ViewController: UIViewController {

    @IBOutlet var nameField: UITextField!
    @IBOutlet var happinessField: UITextField!

    @IBAction func add() {
        if nameField == nil || happinessField == nil {
            return
        }

        let name = nameField!.text
        let happiness = happinessField!.text.Int() // Erro aqui
        if happiness == nil {
            return }
        let meal = Meal(name: name!, happiness: happiness!)
        print("eaten: \(meal.name) \(meal.happiness)")

    }

}
    
asked by anonymous 31.07.2017 / 22:28

2 answers

3

The error is because the structure String has no function called Int() . See the official documentation .

If you are using Swift 1.x

You should use toInt() and not Int() .

let happines = happinessField!.text.toInt()

Or, from Swift 2.x

let happines = Int(happinessField!.text)
    
31.07.2017 / 22:32
0

There is no such function you are trying to access.

If you want to convert a String to Int, you need to use:

let happiness:Int? = Int(happinessField.text)
    
31.07.2017 / 22:32