这个例子中的美元符号有什么作用? [英] What does the dollar sign do in this example?

查看:36
本文介绍了这个例子中的美元符号有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Apple 关于 SwiftUI 的本教程 使用美元符号进行绑定数据,我在 SwiftUI 中无法找到有关此数据绑定的更多信息.

Toggle(isOn: $showFavoritesOnly) {

<块引用>

您可以使用 $ 前缀访问对状态变量或其属性之一的绑定.

这是某种 inout 类型参数吗?这使用与号来传递它.

解决方案

$ 与属性委托结合使用.

不是运算符,而是一个前缀(感谢@matt!).

有关属性委托的更多信息,请参阅此 Swift Evolution 文档.

例如在@State var aState = false中,State是一个属性委托.

这意味着如果我们写:

  • aState 我们正在访问一个 Bool
  • $aState 我们正在访问一个 Binding

不同的属性委托会产生不同的值.

This tutorial by Apple about SwiftUI uses a dollar sign to bind data, and I‘m having trouble finding more information about this data binding in SwiftUI.

Toggle(isOn: $showFavoritesOnly) {

You use the $ prefix to access a binding to a state variable, or one of its properties.

Is this some sort of inout type parameter? That uses the ampersand to pass it on.

解决方案

The $ is used in conjunction with property delegates.

It's not an operator, but a prefix (thanks @matt!).

For more about property delegates, see this Swift Evolution document.

e.g. in @State var aState = false, State is a property delegate.

This means that if we write:

  • aState we're accessing a Bool value
  • $aState we're accessing a Binding<Bool> value

Different property delegates will generate different values.

这篇关于这个例子中的美元符号有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆