swiftswiftuibindingcombineproperty-wrapper

What does the dollar sign do in Swift / SwiftUI?


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.


Solution

  • The $ is used in conjunction with property wrappers (previously known as "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 wrapper.

    This means that if we write:

    Different property delegates will generate different values, called "projected values".