If the Swift 'guard' statement must exit s

2020-02-12 03:31发布

I am confused about the definition of a code block or of 'scope'. Apple docs for guard say this: the else block of a guard statement...

"must transfer control to exit the code block in which the guard statement appear."

Other online sources say the guard statement must exit the 'scope' in which it exists.

So take the example code below:

func testGuardControlFlow () {

let x = 2
let y = 2

    func embededFunc () {

        if y == 2 {

            guard x == 1 else {
                print("oops, number is not 1")
                return
            }

            print ("from in embededFunc")

        }

        print ("I still want this to print even if x != 1")
    }

    embededFunc()
    print("Great, return still allows this to be printed.")

}

testGuardControlFlow()

According to my current understanding of 'scope', the code

if y == 2 {....}

creates a new scope, namely between { }. And given this assumption, guard would merely escape that scope. But that is not the case. Guard in this instance escapes from the function it is placed in, irrespective of whether it is buried in an if clause.

Am I completely misunderstanding what 'scope' means? Does scope mean the code contained in a method? If so, what is the correct term for the 'space' that exists within an if statement?

标签: swift scope
3条回答
男人必须洒脱
2楼-- · 2020-02-12 03:33

You are misinterpreting what the doc says. The guard statement itself doesn't exit any scope. YOU must write some statement inside the guard statement that exits the scope containing the guard statement.

return will exit the scope. In a loop, break or continue will exit the scope. In many cases, goto will exit the stop. There is no specific statement to exit the scope of an if statement, so you would need a goto statement or something that exits the loop or the function containing the guard statement.

查看更多
虎瘦雄心在
3楼-- · 2020-02-12 03:41

It is totally possible to do what you envision, it just happens to not be what that particular code does. return always exits a method, not the local scope. To do what you wish, you can use a label, and break:

func testGuardControlFlow () {

    let x = 2
    let y = 2

    func embededFunc () {

        breakLabel:
        if y == 2 {

            guard x == 1 else {
                print("oops, number is not 1")
                break breakLabel
            }

            print ("from in embededFunc")

        }

        print ("I still want this to print even if x != 1")
    }

    embededFunc()
    print("Great, return still allows this to be printed.")

}

testGuardControlFlow()

To add on to vadian's answer:

guard forces you to exit the scope using a control transfer statement. There are 4 available to you:

  • return and throw both exit the function/method
  • continue can be used within loops (while/for/repeat-while)
  • break can be used in loops (while/for/repeat-while) to exit the immediate scope. Specifying a label to break to will allow you to exit multiple scopes at once (e.g. breaking out of nested loop structure). When using a label, break can also be used in if scopes.

Additionally, you may exit the scope by calling a function that returns Never, such as fatalError.

查看更多
叛逆
4楼-- · 2020-02-12 03:53

Your understanding of the term scope is absolutely correct. Basically it's the space between two (balanced) braces.

The description to exit the code block in the documentation is the most accurate.

To exit a guard statement you can use the control transfer statements return, break, continue, or throw.

  • return and throw exits the whole function or method.
  • continue and break exits the current scope (for example switch, for or while).
查看更多
登录 后发表回答