swift - 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?
解决方案
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!))
推荐阅读
- javascript - 如何将 v-model 数据传递给自定义组件?
- fortran - COMMON 块不会与 Fortran 中本地定义的变量发生名称冲突吗?
- reactjs - 如果单击网格中的任何位置而不是取消,如何避免 ag 网格关闭编辑行
- android - 在具有共享首选项的 Android 应用程序中静音不起作用
- node.js - 不能在 Garuda linux 中使用节点
- debugging - vscode 附加到正在运行的进程
- python - 使用多列从数据框创建字典
- reactjs - NextJs:不同路径的相同类组件,getServerSideProps 不重新渲染
- azure-devops - Azure devops 构建管道似乎两次恢复 nuget 包
- java - ArrayAdapter 后应用程序关闭