SwiftUI中的可选绑定

SwiftUI中的可选绑定,swift,swiftui,Swift,Swiftui,我正在尝试在绑定上创建扩展,以便可以展开并绑定到可选绑定 我有下面的代码,我从StackOverFlow得到的 extension Binding { static func ??<T>(lhs: Binding<Optional<T>>, rhs: T) -> Binding<T> { return Binding( get: { lhs.wrappedValue ?? rhs },

我正在尝试在绑定上创建扩展,以便可以展开并绑定到可选绑定

我有下面的代码,我从StackOverFlow得到的

extension Binding {

    static func ??<T>(lhs: Binding<Optional<T>>, rhs: T) -> Binding<T> {

        return Binding(
            get: { lhs.wrappedValue ?? rhs },
            set: { lhs.wrappedValue = $0 }
        )

    }
}
扩展绑定{
静态函数???(lhs:Binding,rhs:T)->绑定{
返回绑定(
获取:{lhs.wrappedValue??rhs},
集合:{lhs.wrappedValue=$0}
)
}
}
但我得到以下错误:


当您使用
绑定(…)
的初始值设定项时,它将其类型参数推断为
(请记住,
绑定本身是泛型,
是其类型参数),因此实际上它是这样做的:

Binding<Value>(...)
static func ??<T>(lhs: Binding<Optional<T>>, rhs: T) -> Binding<T> {
   .init(get { lhs.wrappedValue ?? rhs },
         set { lhs.wrappedValue = $0 })
}
static func ??(lhs: Binding<Optional<Value>>, rhs: Value) -> Binding<Value> {
   Binding(get { lhs.wrappedValue ?? rhs },
           set { lhs.wrappedValue = $0 })
}