Swift 2 - 在 if 语句中使用 break 的用例?

Swift 2 - Use case for using break on if statement?

Swift 2 的指南提到您可以结束 if 语句的程序执行。我个人从未使用过 break with if 语句。

A break statement ends program execution of a loop, an if statement, or a switch statement...When a break statement is followed by the name of a statement label, it ends program execution of the loop, if statement, or switch statement named by that label.

在什么情况下会在 if 语句中使用 break?这个语言功能好像没什么用。

TEST:
if (true) {
    break TEST
}

将 break 与 if 语句一起使用似乎有点做作,我想不出风格会要求它的地方。但是,它确实在跳过 if-else 子句中 if 语句的后半部分时节省了额外的缩进级别,这对于深度嵌套的循环很有用。

在其他语言中,一种流行的(and/or 有争议的)习惯用法是使用标签来处理深层嵌套函数中的错误。例如,一个人可能想在出现错误时跳出循环,如下所示:

func testBreak3() {
    // doesn't compile!!!
    let a = false, b = true, x = 10, y = 20, err = true
    if !a {
        if b && x > 0 {
            if y < 100 {
                if err {
                    break handleError
                }
                // some statements
            } else {
                // other stuff
            }

        }
    }
    return  // avoid error handling

    handleError:
    print("error")
    // handle the error
}

但在 Swift(我使用 2.0 作为参考)中,标签与其他语言不同;上面的例子没有编译有两个原因:标签在使用时还没有声明,标签必须直接与 dowhileifcase 语句。此外,在 ifdo 语句中中断需要标记该语句。我们可以按如下方式修复此问题,尽管由于通过 errorFlagged 变量进行额外跟踪,这些更改使解决方案的吸引力降低,从而使重构更具吸引力:

func testBreak() {
    let a = false, b = true, x = 10, y = 20, err = true
    var errorFlagged = false
    nestedIf: if !a {
        if b && x > 0 {
            if y < 100 {
                if err {
                    errorFlagged = true
                    break nestedIf
                }
                // some statements
            } else {
                // other stuff
            }
        }
    }

    // skip handling if no error flagged.
    if errorFlagged {
        print("error")
        // handle error
    }
}

例如,如果您想参考数字集(even/rational/negative 数字)描述一个数字(使用字符串),您的代码可能如下所示:

if condition1 {
    // code
    if condition2 {
        // code
        if condition3 {
            // code
            if condition4 {
                //code
            }
        }
    }
}

您可以通过重构(使用 guard)实现相同的逻辑但没有嵌套的 ifs:

OuterIf: if condition1 {
    // code

    guard condition2 else { break OuterIf }
    // code

    guard condition3 else { break OuterIf }
    // code

    guard condition4 else { break OuterIf }
    // code
}

// reads even better when breaking out of "do"
scope: do {
    guard condition1 else { break scope }
    // code

    guard condition2 else { break scope }
    // code

    guard condition3 else { break scope }
    // code

    guard condition4 else { break scope }
    // code

}

您可能认为这也可以通过 switchfallthrough 实现,但这不适用于 "normal" 情况,因为它检查所有条件以及是否满足一个条件以下所有条件甚至都没有被评估。

所以 fallthough 必须有条件地调用。

这确实有效,但我的可读性不是很好,更不用说它了 "beauty":

let x = 4
switch x {
case _ where condition1:
    // code
    if condition2 { fallthrough }
case _ where false:
    // code
    if condition3 { fallthrough }
case _ where false:
    // code
    if condition4 { fallthrough }
case _ where false:
    // code
    break
default: break
}

我知道这是个老话题,但刚才我使用了 break 并且需要它。 所以我的例子 我有一组对象。 当用户点击一个单元格时,i.parameter 对于该单元格中的对象变为 True。 我需要知道数组中的所有对象何时都具有 i.parameter = True ,这就是停止游戏的条件。

func forTimer(){
   for i in array {
    if i.parameter == false {
     break
  }
 }
}
timer = Timer.scheduledTimer(timeInterval: 0.001, target: self, selector: #selector(forTimer), userInfo: nil, repeats: true)

即使一个i.parameter = false,我也不需要检查数组的其余部分。 这个函数每毫秒调用一次,所以我不必每毫秒检查整个数组。