Created
January 17, 2024 16:50
-
-
Save josejuanqm/c56dedd14c1bfb4ec4639307d1595567 to your computer and use it in GitHub Desktop.
Logical AND operator ala Javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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