【问题标题】:Using stride() on UInt in Swift 2.2 will not compile在 Swift 2.2 中的 UInt 上使用 stride() 将无法编译
【发布时间】:2016-03-28 06:15:52
【问题描述】:

我正在尝试对 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 错误还是我做错了什么?

【问题讨论】:

    标签: swift stride swift2.2


    【解决方案1】:

    https://github.com/apple/swift/blob/master/stdlib/public/core/Stride.swift 中声明

    UnsignedIntegerTypes 都有一个带符号的 Stride 类型。

    这是有道理的,否则就不可能从较大的数字迭代到较小的数字。

    因此在你的情况下应该是

    let by = Int(2)
    

    或者只是

    let by = 2
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-18
      • 1970-01-01
      • 2011-06-08
      • 1970-01-01
      • 1970-01-01
      • 2020-01-27
      • 2019-08-17
      • 2019-02-24
      相关资源
      最近更新 更多