使用stride
生成序列时,如何将类型提示传递给Swift以使用Float
而不是Double
?
let floats = Array(stride(from: -160.0, to: 0.0, by: 1.0)) // how to use Float instead of Double?
答案 0 :(得分:1)
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!))
数组通用语法允许这样做:
let floats = Array<Float>(stride(from: -160.0, to: 0.0, by: 1.0))
print(type(of: floats.first!))