The difference is that in the first case, possibleNumber not an optional variable. This is definitely a string. It cannot be nil.
In the second case, textField.text returns an optional string and therefore numberString is an optional variable. It could be zero.
Now ... Conversion Int("") returns an optional int. if the string is "abc", then it cannot return a number, so it returns nil. This is what you deploy with the if let... .
However, in the second case, your string is also optional, and Int () does not accept an optional parameter. So you force it to unfold. This is dangerous, as it can cause the application to crash if the string is zero.
Instead, you can do it ...
if let numberString = textFeidl.text, number = Int(numberString) {
This first expands the text, and if available, use it. Get the number. If it is not zero, you enter a block.
In Swift 2, you can also use the guard let function.
Just saw that you are using Swift 2.
You can do it the same way ...
func getNumber() -> Int { guard let numberString = textField.text, number = Int(numberString) else { return 0 } return number }
source share