JetForMe
(Rick M)
1
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!
Jon_Shier
(Jon Shier)
2
You can use self.$whatever to access them.
1 Like
Lantua
3
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 { ... }
}
}
JetForMe
(Rick M)
4
Ah, I suppose that's reasonable enough. Thanks!
Everything is in the proposal document. 
$ is not an operator, but a compiler reserved prefix, in our case for projected properties.
1 Like
JetForMe
(Rick M)
6
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. ;)
Lantua
8
At least it is mentioned in a form of attribute:
@frozen
@propertyWrapper
@dynamicMemberLookup
struct Binding { ... }
Though the communication could definitely use some improvement.