Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacement for goto statement in Swift

How can I transfer control to a specific line in Swift code?

In Objective-C I would do something like the following, using goto

if(a==b)
{
    goto i123;
}
else
{
    goto i456;
}
NSLog(@"the not reachable point");
i123:
NSLog(@"line 123 is here");
int j = 5;
int x = 2+j;
i456:
NSLog(@"line 456 is here");

The only control transfer statements in Swift I could find were continue, break, fallthrough, and return

continue and break only work with loops; return and fallthrough don't transfer control this way.

What can I use?

EDIT:-

Julien__'s answer didn't actually solve my problem but it could be the only available option right now. so i have accepted the answer by Julien__

like image 954
Pawan Joshi Avatar asked Feb 08 '15 18:02

Pawan Joshi


People also ask

What can I use instead of a goto statement?

From the early days of programming (i.e. assembler programming) the replacement for goto directives is so-called structured programming. This means using if-then-else statements, for- and while-loops, switch statements, break and continue, etc.

Is there a goto in Swift?

Goto. swift is available in the Swift Package Manager.

How can we avoid goto statement?

Use of goto can be simply avoided using break and continue statements.

Is goto still used?

The bottom line is that although goto can almost always be replaced by other forms of logic, it still exists today and is legitimate IDL syntax. There are quite a few hazards to it, but when used sparingly and with care, it can be a useful tool.


3 Answers

Perhaps a switch statement ?

switch (a==b){
default:
    NSLog(@"the not reachable point");
    fallthrough­
case true:
    NSLog(@"line 123 is here");
    int j = 5;
    int x = 2+j;
    fallthrough­
case false:
    NSLog(@"line 456 is here");
}

EDIT : Here is how you could go backward.

let START = 0
let STOP  = -1
var label = START

while(label != STOP){
    switch (label){

    default:
        label = START­

    case START:
        NSLog(@"the not reachable point");
        if a==b {
            label = 123
        } else {
            label = 456
        }

    case 123:
        NSLog(@"line 123 is here");
        int j = 5;
        int x = 2+j;
        fallthrough­

    case 456:
        NSLog(@"line 456 is here");
        fallthrough

    case STOP:
        label = STOP
    }
}

Wrap your code in a giant (but well organized) switch statement. You could even create a function named goto in order to modify the value of the label var.

like image 127
Julien__ Avatar answered Dec 20 '22 03:12

Julien__


Ummm..... Swift actually support label which can be use as goto for control flow but less flexible. The following code is from Swift the programming language :

gameLoop: while square != finalSquare {
    diceRoll += 1
    if diceRoll == 7 { diceRoll = 1 }

    switch square + diceRoll {
    case finalSquare:
    // diceRoll will move us to the final square, so the game is over
        break gameLoop
    case let newSquare where newSquare > finalSquare:
    // diceRoll will move us beyond the final square, so roll again
    continue gameLoop
    default:
    // this is a valid move, so find out its effect
        square += diceRoll
        square += board[square]
    }
}
print("Game over!")
like image 25
Yuji Avatar answered Dec 20 '22 02:12

Yuji


It seems as if Swift doesn't want anyone to use goto statements. Probably to avoid spaghetti code that would be too hard to follow in the future.

One possible alternative is to use functions. Functions have names which have meaning and are easier to understand than mere line numbers.

like image 20
Nima Sepassi Avatar answered Dec 20 '22 03:12

Nima Sepassi