3

First post here so please be gentle. Am fairly new to coding and am trying to get my head around SWIFT and its optionals. Would really appreciate some advice from the pros!

I am writing a simple app whereby textfields are entered by the user and then some multiplication occurs in app before spitting out an answer into another textfield on the press of a button: "calculateTM".

I am having some trouble with the calculation itself and perhaps it's because I am trying to do too much on one line - take the textfield entry, convert to integer, multiply with another textfield entry converted to an integer, essentially what I wrote in the title:

var someVariable: Int = textfield.text.toInt() * textfield2.text.toInt()

The problem is, Xcode is wanting my to force unwrap and add an ! to the end of both toInt(). This is fine, except of course when the user doesn't enter anything into the boxes and presses calculate, at which point the nil value causes the program to crash, e.g.:

var someVariable: Int = textfield.text.toInt() x textfield2.text.toInt()
var someVariable2: Int = textfield3.text.toInt() x textfield4.text.toInt()

where the user doesn't enter anything into textfield3 or 4

Following this simple arithmetic, the code updates the labels (which are textfields) as such:

    label1.text = String(someVariable)
    label2.text = String(someVariable2)

So this final conversion back to a string might also create some issues as to how the optionals are treated in the first part of the code.

Apologies for the long-winded explanation, and I hope I've been clear enough, but I imagine I am missing something really basic with the first part of the code. I have tried using the optional ? and also the nil-coalescing operator (to set to 0 in case of nil) but can't get it to work. Please help?

Many thanks in advance!

Slin S
  • 33
  • 3

2 Answers2

1

Use if let for optional binding:

if let var1 = textField.text?.toInt(),
   let var2 = textField2.text?.toInt() {
 someVariable = var1 * var2 // or directly label1.text = String(var1 * var2)
}
GoZoner
  • 67,920
  • 20
  • 95
  • 145
  • Hi GoZoner. Really appreciate your help with this. However, am having trouble with the " , let var2" bit as it mentions a nested let is not allowed? Does this work within the Swift language? Many thanks! – Slin S Jul 23 '15 at 10:04
  • The above is for Swift 2.0. If you have Swift 1.2 use: `if let var1 = ... { if let var2 = ... { someVariable = var1 * var2 }}`. That is, nest the two `if let` statements. – GoZoner Jul 23 '15 at 18:05
  • 1
    Ah yes I figured out to use the way you described above - it just seemed like excessive code! Seems I need a more updated version of Swift. Many thanks again. Very helpful – Slin S Jul 23 '15 at 21:18
0

The method toInt() actually returns an optional type 'Int?' that can be nil or integer value, so you need to check if the String->Int cast successful returns an Int or a nil.

For the most basic way:

var intValue: Int? = text.toInt()
if intValue != nil {
    // operations using intValue!
}

In swift, you can try:

if let intValue = text.toInt() {
    // operations 
}
  • Thanks for this Tzelann. Appreciate the toInt() explanation, that is v helpful. However, this solution doesn't quite work as I need intValue1 and intValue2 to be multiplied together in the curly brackets so would need to convert both values toInt() before the operation (which will be a simple multiplication) – Slin S Jul 23 '15 at 10:06