Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
166 views
in Technique[技术] by (71.8m points)

swift - 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

    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...