Stefano Gerli
Stefano Gerli

Reputation: 23

SwiftUI Using OR (||) in bindings

Is there any way to use an OR || in a Swift UI binding?

Here's an example:

Class 1:

@State var loading = false
...

Class 2:

@State var loading = false
...

Inside of view:

@ObservedObject var obj1: Class1
@ObservedObject var obj2: Class2
...
...
...
.pullToRefresh(isShowing: self.$obj1.loading || self.$obj2.loading) {
    self.obj1.loadData()
    self.obj2.reloadData()
}

Upvotes: 1

Views: 227

Answers (1)

Asperi
Asperi

Reputation: 258107

You can use extension like below, but pay attention that in such operators you cannot act with binding 'on set', instead you have to operate with components only - binding only reacts

extension Binding where Value == Bool {

    static func ||(_ lhs: Binding<Bool>, _ rhs: Binding<Bool>) -> Binding<Bool> {
        return Binding<Bool>( get: { lhs.wrappedValue || rhs.wrappedValue }, 
                              set: {_ in })
    }
}

Upvotes: 3

Related Questions