Drakalex Drakalex - 23 days ago 8
Swift Question

Have a for loop switching between specified values

I'm working with Swift 3 and Xcode.

I'd like to do something like this :

for i in -3,4,12,0
{ ... }


So i takes the value -3, then 4 then 12 and so on...

I know this is possible in some languages, I saw this syntax :

for v = [1 5 8 17]
disp(v)
end


But is it possible to do it in Swift ?

Answer

You can do either

for i in [-3, 4, 12, 0] {
    //Do whatever you want to do
}

or you do

let myArray = [-3, 4, 12, 0]

for i in myArray {
    //Do whatever you want to do
}

if you want to reuse the array.