Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 358 Vote(s) - 3.71 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to iterate for loop in reverse order in swift?

#11
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
}
Reply

#12
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
Reply

#13
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 `Int`s!

**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
}
Reply

#14
var sum1 = 0
for i in 0...100{
sum1 += i
}
print (sum1)

for i in (10...100).reverse(){
sum1 /= i
}
print(sum1)
Reply

#15
Xcode 6 beta 4 added two functions to iterate on ranges with a step other than one:
`stride(from: to: by:)`, which is used with exclusive ranges and `stride(from: through: by:)`, which is used with inclusive ranges.

To iterate on a range in reverse order, they can be used as below:

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

for index in stride(from: 5, through: 1, by: -1) {
print(index)
}
//prints 5, 4, 3, 2, 1

Note that neither of those is a `Range` member function. They are global functions that return either a `StrideTo` or a `StrideThrough` struct, which are defined differently from the `Range` struct.

A previous version of this answer used the `by()` member function of the `Range` struct, which was removed in beta 4. If you want to see how that worked, check the edit history.
Reply

#16
**as for Swift 2.2 , Xcode 7.3 (10,June,2016) :**

for (index,number) in (0...10).enumerate() {
print("index \(index) , number \(number)")
}

for (index,number) in (0...10).reverse().enumerate() {
print("index \(index) , number \(number)")
}


**Output :**

index 0 , number 0
index 1 , number 1
index 2 , number 2
index 3 , number 3
index 4 , number 4
index 5 , number 5
index 6 , number 6
index 7 , number 7
index 8 , number 8
index 9 , number 9
index 10 , number 10


index 0 , number 10
index 1 , number 9
index 2 , number 8
index 3 , number 7
index 4 , number 6
index 5 , number 5
index 6 , number 4
index 7 , number 3
index 8 , number 2
index 9 , number 1
index 10 , number 0
Reply

#17
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.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through