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__
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.
Goto. swift is available in the Swift Package Manager.
Use of goto can be simply avoided using break and continue statements.
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.
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.
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!")
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With