operator or method in swift which work as walrus operator of python

378 Views Asked by At

walrus operator of python language ( := )
work:- assign the value & also return that value.

language like swift at value assign it return nothing.
how to implement walrus operator kind a thing in swift language ?

I think it done by make function, pass address of variable & value.
assign value in that address & return value.
Is this work or any other way for this?

1

There are 1 best solutions below

1
On

Joakim is correct.

Swift doesn't have a unary operator like C.

In C, you could do:

b = 10;
while (b>0) {
   print(b--);
}

In Swift, there isn't a unary ++ or -- operator, so you would do:

var b = 10
while (b > 0) {
   print b
   b -= 1
}

but, really, in Swift, you'd do this instead

for b in (0...10).reversed() { 
   print b
}

See Reverse Range in Swift