在 Swift 2.2 中对 UInt 使用 stride() 将无法编译
Using stride() on UInt in Swift 2.2 will not compile
我正在尝试对 UInt 变量使用 stride() 函数,但代码无法编译:
let s = UInt(1)
let by = UInt(2)
let to = UInt(10)
for i: UInt in s.stride(to: to, by: by) {
}
编译错误为:
无法使用类型为“(to: UInt, by: UInt)”的参数列表调用 'stride'
Swift 2.2 文档指出它应该是可能的:http://swiftdoc.org/v2.2/type/UInt/#func-stride-to_by_
是 Swift 错误还是我做错了什么?
在https://github.com/apple/swift/blob/master/stdlib/public/core/Stride.swift中指出
The UnsignedIntegerTypes all have a signed Stride type.
这是有道理的,否则不可能从较大的数字迭代到较小的数字。
因此在你的情况下应该是
let by = Int(2)
或者只是
let by = 2
我正在尝试对 UInt 变量使用 stride() 函数,但代码无法编译:
let s = UInt(1)
let by = UInt(2)
let to = UInt(10)
for i: UInt in s.stride(to: to, by: by) {
}
编译错误为:
无法使用类型为“(to: UInt, by: UInt)”的参数列表调用 'stride'
Swift 2.2 文档指出它应该是可能的:http://swiftdoc.org/v2.2/type/UInt/#func-stride-to_by_
是 Swift 错误还是我做错了什么?
在https://github.com/apple/swift/blob/master/stdlib/public/core/Stride.swift中指出
The UnsignedIntegerTypes all have a signed Stride type.
这是有道理的,否则不可能从较大的数字迭代到较小的数字。
因此在你的情况下应该是
let by = Int(2)
或者只是
let by = 2