1

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

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

1 回答 1

1
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!))
于 2018-08-03T10:26:57.033 回答