Bindings to properties with `$self.`

I'm just delving into SwiftUI, and I'm trying to set up my first binding. My Swift formatting style includes always using self. to refer to properties, but it seems I can't do that for bindings:

struct
OpenLocationView : View
{
	@State private var location: String = "https://"
	
	var body: some View
	{
		VStack
		{
			TextField("Location:", text: $self.location)
								         ^~~~~
//	OpenLocationController.swift:33:33: error: use of unresolved identifier '$self'
		}
	}
}

Writing this as $location seems to compile fine.

Clearly I'm misunderstanding something about the $ syntax. Is this particular issue explained anywhere? Thanks!

You can use self.$whatever to access them.

1 Like

Also, it is part of property wrapper from SE-0258. With essentially somewhere in the SwiftUI code base, State is more-or-less declared like this

@propertyWrapper
struct State<Value> {
  var projectedValue: Binding<Value> { ... }
  var wrappedValue: Value {
    get { ... }
    nonmutating set { ... }
  }
}

Ah, I suppose that's reasonable enough. Thanks!

Everything is in the proposal document. :slight_smile:

$ is not an operator, but a compiler reserved prefix, in our case for projected properties.

1 Like

That's not really something you can find without knowing about it a-priori. Imagine someone learning SwiftUI, coming across an example that shows a binding. This person might search for “SwiftUI Binding” and find this document, but that has no link to a higher-level conceptual document, or a usage document, or even the bindings proposal document.

1 Like

In general this is true, but since you‘re an active forums member I guess my answer was good enough. ;)

At least it is mentioned in a form of attribute:

@frozen
@propertyWrapper
@dynamicMemberLookup
struct Binding { ... }

Though the communication could definitely use some improvement.