gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 05:55:12 25 4
gpt4 key购买 nike

当使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com