首页 > 解决方案 > stride(from: 0.0, to: 10.0, by: 2.0) 使用 Float 类型而不是 Double?

问题描述

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

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

标签: swiftsequencelist-comprehensionstride

解决方案


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

improved by comments:

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

this is possible too

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

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

Improved by Sulthan

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

Array Generic syntax allows this:

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

推荐阅读