“.constant”的含义是什么?为什么不使用布尔字面值?

huangapple go评论55阅读模式
英文:

What's the meaning of .constant? Why not Boolean-literal?

问题

以下是要翻译的内容:

Can someone explain this syntax?

static var previews: some View {
    Toast(isToastVisible: .constant(true))
}

What's .constant(true)?

Especially: Why not Toast(isToastVisible: true)?

英文:

Can someone explain this syntax?

static var previews: some View {
    Toast(isToastVisible: .constant(true))
}

What's .constant(true)?

Especially: Why not Toast(isToastVisible: true)?

答案1

得分: 1

isToastVisible属性不是布尔类型,而是Binding<Boolean>,这允许SwiftUI观察视图的更改并根据需要更新UI。

Bindings旨在允许您更新它们包装的值,但在不需要时,例如在预览上,您可以使用常量来初始化它们。

英文:

The isToastVisible property of your View is not a Boolean, it's a Binding<Boolean>, which allows SwiftUI to observe when changes to your view happen, and update the UI as necessary.

Bindings are designed to allow you to update the value they wrap, but when that's not necessary, like on a preview, you can initialise them using a constant instead.

huangapple
  • 本文由 发表于 2023年4月17日 13:01:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76031844.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定