当我在Playground中使用for循环时,一切都很好,直到我将for循环的第一个参数更改为最大值。(按降序迭代)
这是一个bug吗?还有其他人有吗?
for index in 510..509
{
var a = 10
}
显示将要执行的迭代次数的计数器一直在滴答作响……
当我在Playground中使用for循环时,一切都很好,直到我将for循环的第一个参数更改为最大值。(按降序迭代)
这是一个bug吗?还有其他人有吗?
for index in 510..509
{
var a = 10
}
显示将要执行的迭代次数的计数器一直在滴答作响……
当前回答
使用Swift 5,根据您的需要,您可以选择以下四个Playground代码示例中的一个来解决您的问题。
# 1。使用ClosedRange reversed()方法
ClosedRange有一个叫做reversed()的方法。Reversed()方法有以下声明:
func reversed() -> ReversedCollection<ClosedRange<Bound>>
返回以相反顺序显示集合元素的视图。
用法:
let reversedCollection = (0 ... 5).reversed()
for index in reversedCollection {
print(index)
}
/*
Prints:
5
4
3
2
1
0
*/
作为替代,你可以使用Range reversed()方法:
let reversedCollection = (0 ..< 6).reversed()
for index in reversedCollection {
print(index)
}
/*
Prints:
5
4
3
2
1
0
*/
# 2。使用sequence(first:next:)函数
Swift标准库提供了一个名为sequence(first:next:)的函数。Sequence (first:next:)有如下声明:
func sequence<T>(first: T, next: @escaping (T) -> T?) -> UnfoldFirstSequence<T>
返回由first和next的重复惰性应用程序形成的序列。
用法:
let unfoldSequence = sequence(first: 5, next: {
$0 > 0 ? $0 - 1 : nil
})
for index in unfoldSequence {
print(index)
}
/*
Prints:
5
4
3
2
1
0
*/
# 3。使用stride(from:through:by:)函数
Swift标准库提供了一个名为stride(from:through:by:)的函数。Stride (from:through:by:)有如下声明:
func stride<T>(from start: T, through end: T, by stride: T.Stride) -> StrideThrough<T> where T : Strideable
返回一个序列,从起始值开始,并可能包括结束值,按指定的量步进。
用法:
let sequence = stride(from: 5, through: 0, by: -1)
for index in sequence {
print(index)
}
/*
Prints:
5
4
3
2
1
0
*/
作为替代,你可以使用stride(from:to:by:):
let sequence = stride(from: 5, to: -1, by: -1)
for index in sequence {
print(index)
}
/*
Prints:
5
4
3
2
1
0
*/
# 4。使用AnyIterator init(_:)初始化器
AnyIterator有一个名为init(_:)的初始化式。Init(_:)有如下声明:
init(_ body: @escaping () -> AnyIterator<Element>.Element?)
创建一个迭代器,将给定的闭包包装在它的next()方法中。
用法:
var index = 5
guard index >= 0 else { fatalError("index must be positive or equal to zero") }
let iterator = AnyIterator({ () -> Int? in
defer { index = index - 1 }
return index >= 0 ? index : nil
})
for index in iterator {
print(index)
}
/*
Prints:
5
4
3
2
1
0
*/
如果需要,你可以通过为Int创建一个扩展方法并在其中包装迭代器来重构前面的代码:
extension Int {
func iterateDownTo(_ endIndex: Int) -> AnyIterator<Int> {
var index = self
guard index >= endIndex else { fatalError("self must be greater than or equal to endIndex") }
let iterator = AnyIterator { () -> Int? in
defer { index = index - 1 }
return index >= endIndex ? index : nil
}
return iterator
}
}
let iterator = 5.iterateDownTo(0)
for index in iterator {
print(index)
}
/*
Prints:
5
4
3
2
1
0
*/
其他回答
Swift 3更新
下面的答案是可用选项的摘要。选择一个最适合你的需求。
反转:范围内的数字
向前
for index in 0..<5 {
print(index)
}
// 0
// 1
// 2
// 3
// 4
落后的
for index in (0..<5).reversed() {
print(index)
}
// 4
// 3
// 2
// 1
// 0
reversed: SequenceType中的元素
let animals = ["horse", "cow", "camel", "sheep", "goat"]
向前
for animal in animals {
print(animal)
}
// horse
// cow
// camel
// sheep
// goat
落后的
for animal in animals.reversed() {
print(animal)
}
// goat
// sheep
// camel
// cow
// horse
反转:带索引的元素
在迭代一个集合时,有时需要索引。为此,您可以使用enumerate(),它返回一个元组。元组的第一个元素是索引,第二个元素是对象。
let animals = ["horse", "cow", "camel", "sheep", "goat"]
向前
for (index, animal) in animals.enumerated() {
print("\(index), \(animal)")
}
// 0, horse
// 1, cow
// 2, camel
// 3, sheep
// 4, goat
落后的
for (index, animal) in animals.enumerated().reversed() {
print("\(index), \(animal)")
}
// 4, goat
// 3, sheep
// 2, camel
// 1, cow
// 0, horse
请注意,正如Ben Lachman在他的回答中指出的那样,您可能希望使用. enumeration ().reversed()而不是.reversed(). enumeration()(这会使索引数增加)。
步:数字
Stride是一种不使用范围的迭代方法。有两种形式。代码末尾的注释显示了范围版本(假设增量大小为1)。
startIndex.stride(to: endIndex, by: incrementSize) // startIndex..<endIndex
startIndex.stride(through: endIndex, by: incrementSize) // startIndex...endIndex
向前
for index in stride(from: 0, to: 5, by: 1) {
print(index)
}
// 0
// 1
// 2
// 3
// 4
落后的
将增量大小更改为-1允许您后退。
for index in stride(from: 4, through: 0, by: -1) {
print(index)
}
// 4
// 3
// 2
// 1
// 0
注意to和through的区别。
stride: SequenceType的元素
向前递增2
let animals = ["horse", "cow", "camel", "sheep", "goat"]
我在这个例子中使用2只是为了展示另一种可能性。
for index in stride(from: 0, to: 5, by: 2) {
print("\(index), \(animals[index])")
}
// 0, horse
// 2, camel
// 4, goat
落后的
for index in stride(from: 4, through: 0, by: -1) {
print("\(index), \(animals[index])")
}
// 4, goat
// 3, sheep
// 2, camel
// 1, cow
// 0, horse
笔记
@matt有一个有趣的解决方案,他定义了自己的反向运算符,并称之为>>>。它不需要太多代码来定义,并像这样使用: 为索引5>>>0 { 打印(索引) } / / 4 / / 3 / / 2 / / 1 / / 0 查看从Swift 3中删除的c风格For循环
对我来说,这是最好的办法。
var arrayOfNums = [1,4,5,68,9,10]
for i in 0..<arrayOfNums.count {
print(arrayOfNums[arrayOfNums.count - i - 1])
}
对于Swift 2.0及以上版本,你应该在range collection上应用reverse
for i in (0 ..< 10).reverse() {
// process
}
在Swift 3.0中已被重命名为.reversed()
您可以使用reversed()方法轻松反转值。
var i:Int
for i in 1..10.reversed() {
print(i)
}
reversed()方法反转值。
你可以考虑使用c风格的while循环。这在Swift 3中工作得很好:
var i = 5
while i > 0 {
print(i)
i -= 1
}