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
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)
}
toInt()
returns an optional because the conversation to an integer can fail. Consider the case where the user enters something other than a number. When the user enters "Blah", what do you expect toInt()
to return?
You need to conditionally unwrap the optional
if let a = years.text.toInt() {
println(a)
}
I also found forced unwrapping by changing a to a! worked as well. thanks to all who tried to help.