swift - stride(from : 0. 0, to : 10. 0, by : 2. 0) 使用 Float 类型而不是 Double?

标签 swift sequence list-comprehension stride

当使用 stride 生成序列时,如何将类型提示传递给 Swift 以使用 Float 而不是 Double

let floats = Array(stride(from: -160.0, to: 0.0, by: 1.0)) // how to use Float instead of Double?

最佳答案

let floats = Array(stride(from: Float(-160.0), to: Float(0.0), by: Float(1.0)))
print(type(of: floats.first!))

通过评论改进:

let floats = Array(stride(from: Float(-160.0), to:0.0, by: 1.0))
print(type(of: floats.first!))

这也是可能的

let strideTo: StrideTo<Float> = stride(from: -160.0, to: 0.0, by: 1.0)
let floats = Array(strideTo)

print(type(of: floats.first!))

由 Sulthan 改进

let floats = Array(stride(from: -160.0 as Float, to: 0.0, by: 1.0))
print(type(of: floats.first!))

Array Generic 语法允许这样做:

let floats = Array<Float>(stride(from: -160.0, to: 0.0, by: 1.0))
print(type(of: floats.first!))

关于swift - stride(from : 0. 0, to : 10. 0, by : 2. 0) 使用 Float 类型而不是 Double?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51670495/

相关文章:

swift - 如何在 Swift 中格式化 Duration 对象

ios - 访问导致快速崩溃的按钮操作内的单元格

swift - 无法使用参数列表在 Swift 2 中调用 'sendAsynchronousRequest'

r - 根据重置的差异对单调递增的整数序列进行分组

python - 如何使用列表理解来计算列表的累积乘积

python - 不同字符串匹配条件的切片列表

python - 使用列表理解将字符串列表转换为列表列表

xcode - 溢出-Y 滚动不起作用

sql - 在 Oracle 序列中生成相同的正负数

list - 当我们已经有了列表时,为什么 Haskell 需要 Data.Sequence?