我正在将代码从 java 重写为 swift,并且需要以正确的方式打破多层开关。在 Java 中它看起来像:
block0 : switch (topSwithch) {
case one: {
switch (innerSwitch) {
case insideCase: {
if (something){
break block0;
}
// etc
因此,我要断开一个开关与另一个开关的连接。我如何在 swift 中做到这一点?
最佳答案
这就是它在 Swift 中的样子。我在 Playground 上用变量的硬编码值做了这个:
let topSwitch = 1
let innerSwitch = 4
let one = 1
let insideCase = 4
let something = true
block0 : switch (topSwitch) { //first switch labeled "block0", switching on topSwitch
case one: //topSwitch is 1, one is 1
switch (innerSwitch) { // switching on innerSwitch
case insideCase: // both are 1
if (something){ // if true
break block0; // break from "block0"
}
default: break // else
}
default: break // else
}
关于Swift正确破解多层开关,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39374442/