当我在Playground中使用for循环时,一切都很好,直到我将for循环的第一个参数更改为最大值。(按降序迭代)
这是一个bug吗?还有其他人有吗?
for index in 510..509
{
var a = 10
}
显示将要执行的迭代次数的计数器一直在滴答作响……
当我在Playground中使用for循环时,一切都很好,直到我将for循环的第一个参数更改为最大值。(按降序迭代)
这是一个bug吗?还有其他人有吗?
for index in 510..509
{
var a = 10
}
显示将要执行的迭代次数的计数器一直在滴答作响……
当前回答
如果希望反向遍历数组(array或更一般的任何SequenceType)。您还有一些额外的选项。
首先,您可以reverse()数组并像往常一样遍历它。但是我更喜欢使用enumerate(),因为它输出一个包含对象和它的索引的元组。
这里需要注意的一件事是,以正确的顺序调用它们是很重要的:
For (index, element) in array.enumerate().reverse()
按降序生成索引(这是我通常期望的)。而:
for (index, element) in array.reverse().enumerate()(与NSArray的reverseEnumerator更接近)
反向遍历数组,但输出升序索引。
其他回答
对于Swift 2.0及以上版本,你应该在range collection上应用reverse
for i in (0 ..< 10).reverse() {
// process
}
在Swift 3.0中已被重命名为.reversed()
使用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
*/
Xcode 6 beta 4增加了两个函数来迭代除一个步骤以外的范围: Stride (from: to: by:)用于独占范围,Stride (from: through: by:)用于包含范围。
要以相反的顺序迭代一个范围,它们可以如下所示:
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
注意,这两个都不是Range成员函数。它们是返回StrideTo或StrideThrough结构体的全局函数,它们与Range结构体的定义不同。
这个答案的前一个版本使用了Range结构体的by()成员函数,该函数在beta 4中被删除。如果您想了解它是如何工作的,请检查编辑历史记录。
你可以考虑使用c风格的while循环。这在Swift 3中工作得很好:
var i = 5
while i > 0 {
print(i)
i -= 1
}
这将以相反的顺序递减1。
let num1 = [1,2,3,4,5]
for item in nums1.enumerated().reversed() {
print(item.offset) // Print the index number: 4,3,2,1,0
print(item.element) // Print the value :5,4,3,2,1
}
或者你可以用这个index value属性
let num1 = [1,2,3,4,5]
for (index,item) in nums1.enumerated().reversed() {
print(index) // Print the index number: 4,3,2,1,0
print(item) // Print the value :5,4,3,2,1
}