Swift 中的反向范围 [英] Reverse Range in Swift

查看:22
本文介绍了Swift 中的反向范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在 Swift 中使用反向范围?

例如:

for i in 5...1 {//做点什么}

是一个无限循环.

在较新版本的 Swift 中,代码可以编译,但在运行时出现错误:

<块引用>

致命错误:无法形成带有 upperBound

我知道我可以使用 1..5 代替,计算 j = 6 - i 并使用 j 作为我的索引.我只是想知道有没有更清晰的东西?

解决方案

更新最新的 Swift 3(仍然适用于 Swift 4)

您可以在范围上使用 reversed() 方法

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

或者stride(from:through:by:)方法

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

stide(from:to:by:) 类似但不包括最后一个值

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

更新最新的 Swift 2

首先,协议扩展改变了reverse的使用方式:

for i in (1...5).reverse() { print(i) }//5 4 3 2 1

Stride 已在 Xcode 7 Beta 6 中重新设计.新用法是:

for i in 0.stride(to: -8, by: -2) { print(i) }//0 -2 -4 -6for i in 0.stride(through: -8, by: -2) { print(i) }//0 -2 -4 -6 -8

它也适用于双打:

for i in 0.5.stride(to:-0.1, by: -0.1) { print(i) }

小心浮点数比较这里的界限.

Swift 1.2 的早期编辑:从 Xcode 6 Beta 4 开始,byReverseRange 不再存在:[

如果您只是想反转一个范围,reverse 函数就是您所需要的:

for i in reverse(1...5) { println(i) }//打印 5,4,3,2,1

正如 0x7fffffff 发布的那样,有一个新的 stride 构造,可用于迭代和递增任意整数.Apple 还表示即将提供浮点支持.

来自他的回答:

 for x in stride(from: 0, through: -8, by: -2) {println(x)//0, -2, -4, -6, -8}对于 x 步幅(从:6,到:-2,由:-4){println(x)//6, 2}

Is there a way to work with reverse ranges in Swift?

For example:

for i in 5...1 {
  // do something
}

is an infinite loop.

In newer versions of Swift that code compiles, but at runtime gives the error:

Fatal error: Can't form Range with upperBound < lowerBound

I know I can use 1..5 instead, calculate j = 6 - i and use j as my index. I was just wondering if there was anything more legible?

解决方案

Update For latest Swift 3 (still works in Swift 4)

You can use the reversed() method on a range

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

Or stride(from:through:by:) method

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

stide(from:to:by:) is similar but excludes the last value

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

Update For latest Swift 2

First of all, protocol extensions change how reverse is used:

for i in (1...5).reverse() { print(i) } // 5 4 3 2 1

Stride has been reworked in Xcode 7 Beta 6. The new usage is:

for i in 0.stride(to: -8, by: -2) { print(i) } // 0 -2 -4 -6
for i in 0.stride(through: -8, by: -2) { print(i) } // 0 -2 -4 -6 -8

It also works for Doubles:

for i in 0.5.stride(to:-0.1, by: -0.1) { print(i) }

Be wary of floating point compares here for the bounds.

Earlier edit for Swift 1.2: As of Xcode 6 Beta 4, by and ReverseRange don't exist anymore :[

If you are just looking to reverse a range, the reverse function is all you need:

for i in reverse(1...5) { println(i) } // prints 5,4,3,2,1

As posted by 0x7fffffff there is a new stride construct which can be used to iterate and increment by arbitrary integers. Apple also stated that floating point support is coming.

Sourced from his answer:

for x in stride(from: 0, through: -8, by: -2) {
    println(x) // 0, -2, -4, -6, -8
}

for x in stride(from: 6, to: -2, by: -4) {
    println(x) // 6, 2
}

这篇关于Swift 中的反向范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆