在快速c型循环被弃用后循环中的递减索引

时间:2021-08-25 22:25:38

How would you express a decrementing indexed loop in Swift 3.0, where the syntax below is not valid any more?

在Swift 3.0中,如果下面的语法不再有效,您将如何表示一个递减的索引循环?

for var index = 10 ; index > 0; index-=1{
   print(index)
}

// 10 9 8 7 6 5 4 3 2 1

5 个解决方案

#1


29  

Here is an easier (and more Swifty) approach.

这里有一个更简单(也更敏捷)的方法。

for i in (0 ..< 5).reversed() {
    print(i) // 4,3,2,1,0
}

let array = ["a", "b", "c", "d", "e"]
for element in array.reversed() {
    print(element) // e,d,c,b,a
}

array.reversed().forEach { print($0) } // e,d,c,b,a

print(Array(array.reversed())) // e,d,c,b,a

#2


46  

C-style loops with a fixed increment or decrement can be replaced by stride():

带有固定增量或递减的c型循环可以用stride()代替:

for index in 10.stride(to: 0, by: -1) {
    print(index)
}

// 10 9 8 7 6 5 4 3 2 1

Use stride(to: ...) or stride(through: ...) depending on whether the last element should be included or not.

使用stride(to:…)或stride(through:…),这取决于最后一个元素是否应该包含。

This is for Swift 2. The syntax changed (again) for Swift 3, see this answer.

这是给斯威夫特2的。Swift 3的语法(再次)发生了变化,请参见此答案。

#3


17  

From swift 3.0, The stride(to:by:) method on Strideable has been replaced with a free function, stride(from:to:by:)

从swift 3.0开始,Strideable上的stride(to:by:)方法已经被一个free function所取代,stride(From:to:by:)

 for index in stride(from: 10, to: 0, by: -1){
      print(index)
 }

#4


9  

You can use stride method:

你可以使用步法:

10.stride(through: 0, by: -1).forEach { print($0) }

or classic while loop.

或经典的while循环。

#5


1  

If you still want to use this C-style loop, here is what you need:

如果你还想使用这个c型循环,以下是你需要的:

let x = 10

infix operator ..> { associativity left }
func ..>(left: Int, right: Int) -> StrideTo<Int> {
    return stride(from: left, to: right, by: -1)
}

for i in x..>0 {
    print(i)
}

#1


29  

Here is an easier (and more Swifty) approach.

这里有一个更简单(也更敏捷)的方法。

for i in (0 ..< 5).reversed() {
    print(i) // 4,3,2,1,0
}

let array = ["a", "b", "c", "d", "e"]
for element in array.reversed() {
    print(element) // e,d,c,b,a
}

array.reversed().forEach { print($0) } // e,d,c,b,a

print(Array(array.reversed())) // e,d,c,b,a

#2


46  

C-style loops with a fixed increment or decrement can be replaced by stride():

带有固定增量或递减的c型循环可以用stride()代替:

for index in 10.stride(to: 0, by: -1) {
    print(index)
}

// 10 9 8 7 6 5 4 3 2 1

Use stride(to: ...) or stride(through: ...) depending on whether the last element should be included or not.

使用stride(to:…)或stride(through:…),这取决于最后一个元素是否应该包含。

This is for Swift 2. The syntax changed (again) for Swift 3, see this answer.

这是给斯威夫特2的。Swift 3的语法(再次)发生了变化,请参见此答案。

#3


17  

From swift 3.0, The stride(to:by:) method on Strideable has been replaced with a free function, stride(from:to:by:)

从swift 3.0开始,Strideable上的stride(to:by:)方法已经被一个free function所取代,stride(From:to:by:)

 for index in stride(from: 10, to: 0, by: -1){
      print(index)
 }

#4


9  

You can use stride method:

你可以使用步法:

10.stride(through: 0, by: -1).forEach { print($0) }

or classic while loop.

或经典的while循环。

#5


1  

If you still want to use this C-style loop, here is what you need:

如果你还想使用这个c型循环,以下是你需要的:

let x = 10

infix operator ..> { associativity left }
func ..>(left: Int, right: Int) -> StrideTo<Int> {
    return stride(from: left, to: right, by: -1)
}

for i in x..>0 {
    print(i)
}