当我在Playground中使用for循环时,一切都很好,直到我将for循环的第一个参数更改为最大值。(按降序迭代)
这是一个bug吗?还有其他人有吗?
for index in 510..509
{
var a = 10
}
显示将要执行的迭代次数的计数器一直在滴答作响……
当我在Playground中使用for循环时,一切都很好,直到我将for循环的第一个参数更改为最大值。(按降序迭代)
这是一个bug吗?还有其他人有吗?
for index in 510..509
{
var a = 10
}
显示将要执行的迭代次数的计数器一直在滴答作响……
当前回答
反转一个数组只需要一个步骤。reverse()
var arrOfnum = [1,2,3,4,5,6]
arrOfnum.reverse()
其他回答
你可以考虑使用c风格的while循环。这在Swift 3中工作得很好:
var i = 5
while i > 0 {
print(i)
i -= 1
}
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循环
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中被删除。如果您想了解它是如何工作的,请检查编辑历史记录。
在Swift 4及后续版本中
let count = 50//For example
for i in (1...count).reversed() {
print(i)
}
对我来说,这是最好的办法。
var arrayOfNums = [1,4,5,68,9,10]
for i in 0..<arrayOfNums.count {
print(arrayOfNums[arrayOfNums.count - i - 1])
}