很快我就可以用 Int(doubleVariable) 将 Int 转换为 Double 。
但是如何将 Binding 转换为 Binding 呢?
然后我可以将 @Binding var intVar: Int 传递给需要 Double 绑定的函数。
(这个上下文是我有一个 Int,我想将其传递给 SwiftUI 中的 Slider。)
我认为没有办法进行强制转换,但您可以基于第一个绑定创建另一个绑定:
import SwiftUI
var d = 1.0
// Create a Binding<Double> (for lack of another source)
var bd = Binding<Double>(get: { d }, set: { d = $0 })
// "Convert" to Binding<Int> by recreating another Binding
let bi = Binding<Int>(get: { Int(bd.wrappedValue) },
set: { bd.wrappedValue = Double($0) })
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)