Don Alejandro Don Alejandro - 5 months ago 25
Swift Question

Where does this line of code return to?

In the code below, I'm confused as to where the

return
statement in the code returns to? When executed, it works as expected, but does it return to:

if userIsInTheMiddleOfTyping == true


or does it return to:

if let digit = sender.currentTitle


Below is the full chunk of code where this applies.

class ViewController: UIViewController {

private var userIsInTheMiddleOfTyping = false
private var decimalUsed = false

@IBAction private func touchDigit(sender: UIButton)
{
if let digit = sender.currentTitle {

if userIsInTheMiddleOfTyping == true {

if digit == "." && decimalUsed == true {
return //where does this return to?
} else if digit == "." && decimalUsed == false {
decimalUsed = true
}


let textCurrentlyInDisplay = display.text!
display.text = textCurrentlyInDisplay + digit

} else {
display.text = digit
}

userIsInTheMiddleOfTyping = true

}
}

Answer

A return always returns out of the function, so in this case it returns to the line of code that calls touchDigit(...)

Basically here, the return just stops the execution of the touchDigit function.

(Which means that none of the code following the return will be run)