Last active
September 5, 2019 16:06
-
-
Save zakbarlow1995/3ca2f31562cf72eb576ab92ccd7b58e3 to your computer and use it in GitHub Desktop.
Handy quality of life infix operator for string interpolation of optionals
This file contains 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
infix operator ???: NilCoalescingPrecedence | |
public func ??? <T>(optional: T?, defaultValue: @autoclosure () -> String) -> String { | |
return optional.map { String(describing: $0) } ?? defaultValue() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Source: "Using optionals in string interpolation segments can lead to surprising or unwanted results. I defined a custom operator that lets me provide a default string value for optionals that are nil."
Usage: