Suppose I have an array of var code = [25, 17, 46, 23, 55]
and I need to derive data from it.
You can do this withfor var i = code.count; i<0; i-=1 {print ("\(code[i])")}
for var i = code.count; i<0; i-=1 {print ("\(code[i])")}
but xcode says that the c ++ style will be deleted soon and does not even output the result to me.
Do I have an error in the code, and if not, how else can I output the data in the reverse order?
|
1 answer
In your example code, two errors:
for var i = code.count-1; i>=0; i-=1 {print ("\(code[i])")}
You can rewrite it as follows:
for i in (0...code.count-1).reverse() { print ("\(code[i])") }
- Natasha, by the way, has an excellent article on this topic. Natashatherobot.com/swift-alternatives-to-c-style-for-loops - Max Mikheyenko
|