使用新的@Binding委托和预览,我总是不得不创建一个@State static var来创建neccesarry绑定,这有点尴尬:
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
Run Code Online (Sandbox Code Playgroud)
有没有更简单的方法来创建绑定,该绑定代理用于测试和预览的简单值?
您可以.constant(VALUE)在“预览”中使用,而无需创建@State。
/// A value and a means to mutate it.
@propertyDelegate public struct Binding<Value> {
/// Creates a binding with an immutable `value`.
public static func constant(_ value: Value) -> Binding<Value>
}
Run Code Online (Sandbox Code Playgroud)
例如
TestView(someProperty: .constant(5.0))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
507 次 |
| 最近记录: |