使用浮动类型而不是Double跨越(从:0.0,到:10.0,通过:2.0)

时间:2018-08-03 10:19:16

标签: 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?

1 个答案:

答案 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!))