Swift giving Optional(3) instead of 3 for .toInt

前端 未结 3 1038
迷失自我
迷失自我 2021-01-29 13:51

Trying to pull a number from a Field and keep getting Optional(number) instead of the number

@IBOutlet weak var years: UITextField!

@IBAction func calculateYear         


        
3条回答
  •  野的像风
    2021-01-29 14:05

    That happens because the toInt() method returns an optional, to account for the case when the string is not convertible to an integer.

    Just put that in an optional binding:

    if let a = a {
        println(a)
    }
    

提交回复
热议问题