Swift `for-in` 循环范围和 ClosedRange 错误
Swift `for-in` loop Range and ClosedRange errors
我正在尝试编写简单的 Swift for
循环,如下所示:
for i in [0...10] {
// ...
}
for i in [0..<10] {
// ...
}
由于 Swift 中缺少经典的 C 风格 for
循环,因此这些 for-in
范围内的循环很重要。我正在尝试枚举通过 Swift 的 ...
和 ..<
语法构建的任何 Ints 数组。
请注意,如果我构造一个整数数组 "manually" 而不是使用 [...]
或 [..<]
,它工作正常:
for i in [0, 1, 2, 3, 4, 5] {
// this works fine
}
但是 [0..<5]
循环基本上每次我尝试使用值 i
:
时都会产生错误
let _ = i + 1
这是我得到的错误:
- Binary operator `'+'` cannot be applied to operands of type `'ClosedRange<Int>'` and `'Int'`
- Binary operator `'+'` cannot be applied to operands of type `'Range<Int>'` and `'Int' `
我也试过这个:
let _ = String(i)
然后我得到:
Ambiguous reference to initializer `'init(_:)'`
为什么这些简单的 for-in
循环不起作用?
为什么错误引用 Range
和 ClosedRange
?我期待 Int
.
Swift范围写成0...10
,不是[0...10]
.
[0...10]
创建单项数组。数组中的第一项是 0...10
范围。
因此使用 for i in [0...10]
遍历该单项数组,而不是 范围本身。迭代值 i
的类型为 Range
或 ClosedRange
.
要遍历范围内的每个 Int
,正如您期望的那样,使用范围 0...10
不带 括号:
for i in 0...10 {
// ...
}
for i in 0..<10 {
// ...
}
https://docs.swift.org/swift-book/LanguageGuide/ControlFlow.html
You can also use for-in loops with numeric ranges. This example prints the first few entries in a five-times table:
for index in 1...5 {
print("\(index) times 5 is \(index * 5)")
}
The sequence being iterated over is a range of numbers from 1 to 5, inclusive, as indicated by the use of the closed range operator (...)
.
范围
Swift 中有一个类型叫做 Range
。它是一个 struct
,可以像这样使用:
Range.init(uncheckedBounds: (lower: 0, upper: 10))
由于在代码中使用范围很常见,因此有一个语法糖,例如:
0..<10
所以当你说 [0...10]
就像你说 [Range.init(uncheckedBounds: (lower: 0, upper: 10))]
而那不是你要找的。
相反,您应该像这样迭代 range
本身:
for i in 0...10 {
print(i)
}
额外:
ClosedRange
类似于 Range
当然你可以去掉 Swift 中的 init
关键字:
ClosedRange(uncheckedBounds: (lower: 0, upper: 10)) // 0...10
我正在尝试编写简单的 Swift for
循环,如下所示:
for i in [0...10] {
// ...
}
for i in [0..<10] {
// ...
}
由于 Swift 中缺少经典的 C 风格 for
循环,因此这些 for-in
范围内的循环很重要。我正在尝试枚举通过 Swift 的 ...
和 ..<
语法构建的任何 Ints 数组。
请注意,如果我构造一个整数数组 "manually" 而不是使用 [...]
或 [..<]
,它工作正常:
for i in [0, 1, 2, 3, 4, 5] {
// this works fine
}
但是 [0..<5]
循环基本上每次我尝试使用值 i
:
let _ = i + 1
这是我得到的错误:
- Binary operator `'+'` cannot be applied to operands of type `'ClosedRange<Int>'` and `'Int'`
- Binary operator `'+'` cannot be applied to operands of type `'Range<Int>'` and `'Int' `
我也试过这个:
let _ = String(i)
然后我得到:
Ambiguous reference to initializer `'init(_:)'`
为什么这些简单的 for-in
循环不起作用?
为什么错误引用 Range
和 ClosedRange
?我期待 Int
.
Swift范围写成0...10
,不是[0...10]
.
[0...10]
创建单项数组。数组中的第一项是 0...10
范围。
因此使用 for i in [0...10]
遍历该单项数组,而不是 范围本身。迭代值 i
的类型为 Range
或 ClosedRange
.
要遍历范围内的每个 Int
,正如您期望的那样,使用范围 0...10
不带 括号:
for i in 0...10 {
// ...
}
for i in 0..<10 {
// ...
}
https://docs.swift.org/swift-book/LanguageGuide/ControlFlow.html
You can also use for-in loops with numeric ranges. This example prints the first few entries in a five-times table:
for index in 1...5 { print("\(index) times 5 is \(index * 5)") }
The sequence being iterated over is a range of numbers from 1 to 5, inclusive, as indicated by the use of the closed range operator
(...)
.
范围
Swift 中有一个类型叫做 Range
。它是一个 struct
,可以像这样使用:
Range.init(uncheckedBounds: (lower: 0, upper: 10))
由于在代码中使用范围很常见,因此有一个语法糖,例如:
0..<10
所以当你说 [0...10]
就像你说 [Range.init(uncheckedBounds: (lower: 0, upper: 10))]
而那不是你要找的。
相反,您应该像这样迭代 range
本身:
for i in 0...10 {
print(i)
}
额外:
ClosedRange
类似于 Range
当然你可以去掉 Swift 中的 init
关键字:
ClosedRange(uncheckedBounds: (lower: 0, upper: 10)) // 0...10