user2976267 user2976267 - 2 months ago 19
Swift Question

Swift correct break multi-layer switch

I'm rewriting a code from java to swift and need to break a multi-layered switches right way. In Java it looks like :

block0 : switch (topSwithch) {
case one: {
switch (innerSwitch) {
case insideCase: {
if (something){
break block0;
}
// etc


So, I'm breaking one switch from another. How do I do it in swift ?

Answer

This is what it'd look like in Swift. I did this in a playground with hardcoded values for the variables:

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
}
Comments