Skip to content

Instantly share code, notes, and snippets.

@josejuanqm
Created January 17, 2024 16:50
Show Gist options
  • Select an option

  • Save josejuanqm/c56dedd14c1bfb4ec4639307d1595567 to your computer and use it in GitHub Desktop.

Select an option

Save josejuanqm/c56dedd14c1bfb4ec4639307d1595567 to your computer and use it in GitHub Desktop.
Logical AND operator ala Javascript
protocol Delegate: AnyObject {
}
class A {
weak var delegate: Delegate?
}
class B: Delegate {
}
let delegateClass: Delegate = B()
let booleanCheck = UIDevice.current.userInterfaceIdiom == .phone // is it a phone?
let foo = A()
foo.delegate = booleanCheck && delegateClass // This returns a Delegate? (Optional<Delegate> object)
import Foundation
infix operator &&: AdditionPrecedence
func && <T>(lhs: Bool, rhs: T) -> T? {
lhs ? rhs : nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment