SwiftUI:如何在“$”可绑定对象运算符之前使用“!”运算符?

Mas*_*ntX 4 xcode ios swift swiftui

我无法对!Bindable$对象使用逻辑非运算符。

这是我想要的场景 -

struct ContentView: View {
@State private var isLoggedIn:Bool = true
var body: some View {

    Text("Root View")
        .sheet(isPresented: !self.$isLoggedIn) {
            SignInView()
        }
        .onAppear { self.performAuthentication() }
   }
}
Run Code Online (Sandbox Code Playgroud)

isLoggedIn = false当我通过某些按钮操作进行设置时,登录视图应该立即显示。为此我必须先使用逻辑非运算符$

编译器错误:无法将“Binding”类型的值转换为预期的参数类型“Bool”

我怎样才能实现这个目标?

Asp*_*eri 6

正如我在问题评论中所说,有针对SwiftUI 的方法:将 Binding 转换为另一个 Binding。但是,如果您希望将其明确作为运算符,则可以使用以下内容(经过测试并适用于 Xcode 11.2)

extension Binding where Value == Bool {
    static prefix func !(_ lhs: Binding<Bool>) -> Binding<Bool> {
        return Binding<Bool>(get:{ !lhs.wrappedValue }, 
                             set: { lhs.wrappedValue = !$0})
    }
}
Run Code Online (Sandbox Code Playgroud)