如果 Swift 'guard' 语句必须退出作用域,那么作用域的定义是什么? [英] If the Swift 'guard' statement must exit scope, what is the definition of scope?

查看:32
本文介绍了如果 Swift 'guard' 语句必须退出作用域,那么作用域的定义是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对代码块或范围"的定义感到困惑.苹果的守卫文档是这样说的:守卫语句的 else 块......

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."

其他 在线资源 说 guard 语句必须退出它所在的范围".

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

请看下面的示例代码:

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 {....}

创建一个新的作用域,即在 { } 之间.考虑到这个假设,guard 只会逃离那个范围.但事实并非如此.在这种情况下,Guard 会从它所在的函数中逃逸,而不管它是否被埋没在 if 子句中.

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.

我是否完全误解了范围"的含义?范围是否意味着包含在方法中的代码?如果是这样,if 语句中存在的空格"的正确术语是什么?

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?

推荐答案

完全有可能实现您的设想,只是碰巧不是特定代码所做的.return 总是退出一个方法,而不是本地范围.为了做你想做的事,你可以使用标签,然后 break:

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()

添加到 vadian 的回答:

guard 强制您使用控制转移语句退出作用域.有 4 个可供您使用:

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

  • returnthrow 都退出函数/方法
  • continue 可以在循环中使用 (while/for/repeat-while)
  • break 可以在循环(while/for/repeat-while)中使用以退出立即范围.指定要中断的标签将允许您一次退出多个范围(例如,中断嵌套循环结构).使用标签时,break 也可用于 if 范围.
  • 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.

此外,您可以通过调用返回 Never,例如fatalError.

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

这篇关于如果 Swift 'guard' 语句必须退出作用域,那么作用域的定义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆