Binding to a variable bug?

Looks like bug to me:

struct Foo {
    static var bar: Int = 0
}

let binding1: Binding<Int> = .init { Foo.bar } set: { Foo.bar = $0 } // ok
let binding2: Binding<Int> = $Foo.bar // Error: Cannot find '$Foo' in scope

oops, it doesn't work even here:

var x = 0
let y: Binding<Int> = .init { x } set: { x = $0 } // ok
let z: Binding<Int> = $x // Error: Cannot find '$x' in scope

not a bug, feature?

You may want to ask over #swift-users instead; that may help garner the more appropriate group of audience. In any case, the syntax $varName currently applies to property wrappers only (SE-0258 and SE-0293)

1 Like

What do you expect $Foo to refer to? The $ prefix is added to a property wrapper in order to access its projected value. Read the documentation.

Terms of Service

Privacy Policy

Cookie Policy