英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论