如何为预览创建简单的绑定
How to create a simple Binding for previews
有了新的 @Binding
委托和预览,我发现总是必须创建一个 @State static var
来创建必要的绑定有点尴尬:
struct TestView: View {
@Binding var someProperty: Double
var body: some View {
//...
}
}
#if DEBUG
struct TestView_Previews : PreviewProvider {
@State static var someProperty = 0.7
static var previews: some View {
TestView(someProperty: $someProperty)
}
}
#endif
是否有更简单的方法来创建绑定,代理用于测试和预览的简单值?
您可以在预览中使用 .constant(VALUE)
,无需创建 @State
。
/// A value and a means to mutate it.
@propertyWrapper public struct Binding<Value> {
/// Creates a binding with an immutable `value`.
public static func constant(_ value: Value) -> Binding<Value>
}
例如
TestView(someProperty: .constant(5.0))
有了新的 @Binding
委托和预览,我发现总是必须创建一个 @State static var
来创建必要的绑定有点尴尬:
struct TestView: View {
@Binding var someProperty: Double
var body: some View {
//...
}
}
#if DEBUG
struct TestView_Previews : PreviewProvider {
@State static var someProperty = 0.7
static var previews: some View {
TestView(someProperty: $someProperty)
}
}
#endif
是否有更简单的方法来创建绑定,代理用于测试和预览的简单值?
您可以在预览中使用 .constant(VALUE)
,无需创建 @State
。
/// A value and a means to mutate it.
@propertyWrapper public struct Binding<Value> {
/// Creates a binding with an immutable `value`.
public static func constant(_ value: Value) -> Binding<Value>
}
例如
TestView(someProperty: .constant(5.0))