How to iterate for loop in reverse order in swift?

2019-01-02 17:17发布

When I use the for loop in Playground, everything worked fine, until I changed the first parameter of for loop to be the highest value. (iterated in descending order)

Is this a bug? Did any one else have it?

for index in 510..509
{
    var a = 10
}

The counter that displays the number of iterations that will be executions keeps ticking...

enter image description here

13条回答
何处买醉
2楼-- · 2019-01-02 17:56

If one is wanting to iterate through an array (Array or more generally any SequenceType) in reverse. You have a few additional options.

First you can reverse() the array and loop through it as normal. However I prefer to use enumerate() much of the time since it outputs a tuple containing the object and it's index.

The one thing to note here is that it is important to call these in the right order:

for (index, element) in array.enumerate().reverse()

yields indexes in descending order (which is what I generally expect). whereas:

for (index, element) in array.reverse().enumerate() (which is a closer match to NSArray's reverseEnumerator)

walks the array backward but outputs ascending indexes.

查看更多
看风景的人
3楼-- · 2019-01-02 17:57

For Swift 2.0 and above you should apply reverse on a range collection

for i in (0 ..< 10).reverse() {
  // process
}

It has been renamed to .reversed() in Swift 3.0

查看更多
低头抚发
4楼-- · 2019-01-02 17:58

You can consider using the C-Style while loop instead. This works just fine in Swift 3:

var i = 5 
while i > 0 { 
    print(i)
    i -= 1
}
查看更多
不流泪的眼
5楼-- · 2019-01-02 18:00
var sum1 = 0
for i in 0...100{
    sum1 += i
}
print (sum1)

for i in (10...100).reverse(){
    sum1 /= i
}
print(sum1)
查看更多
骚的不知所云
6楼-- · 2019-01-02 18:02

Swift 4.0

for i in stride(from: 5, to: 0, by: -1) {
    print(i) // 5,4,3,2,1
}

If you want to include the to value:

for i in stride(from: 5, through: 0, by: -1) {
    print(i) // 5,4,3,2,1,0
}
查看更多
几人难应
7楼-- · 2019-01-02 18:05

Apply the reverse function to the range to iterate backwards:

For Swift 1.2 and earlier:

// Print 10 through 1
for i in reverse(1...10) {
    println(i)
}

It also works with half-open ranges:

// Print 9 through 1
for i in reverse(1..<10) {
    println(i)
}

Note: reverse(1...10) creates an array of type [Int], so while this might be fine for small ranges, it would be wise to use lazy as shown below or consider the accepted stride answer if your range is large.


To avoid creating a large array, use lazy along with reverse(). The following test runs efficiently in a Playground showing it is not creating an array with one trillion Ints!

Test:

var count = 0
for i in lazy(1...1_000_000_000_000).reverse() {
    if ++count > 5 {
        break
    }
    println(i)
}

For Swift 2.0 in Xcode 7:

for i in (1...10).reverse() {
    print(i)
}

Note that in Swift 2.0, (1...1_000_000_000_000).reverse() is of type ReverseRandomAccessCollection<(Range<Int>)>, so this works fine:

var count = 0
for i in (1...1_000_000_000_000).reverse() {
    count += 1
    if count > 5 {
        break
    }
    print(i)
}

For Swift 3.0 reverse() has been renamed to reversed():

for i in (1...10).reversed() {
    print(i) // prints 10 through 1
}
查看更多
登录 后发表回答