Skip to content

Instantly share code, notes, and snippets.

@kingiol
Last active September 1, 2018 15:14
Show Gist options
  • Save kingiol/09e8b30e45d5d30d4dc70ecedcdf997c to your computer and use it in GitHub Desktop.
Save kingiol/09e8b30e45d5d30d4dc70ecedcdf997c to your computer and use it in GitHub Desktop.
Can print any object to String
infix operator ???: NilCoalescingPrecedence
public func ???<T>(optional: T?, defaultValue: @autoclosure () -> String) -> String {
switch optional {
case let value?:
return String(describing: value)
case nil:
return defaultValue()
}
}
let x = "123" ??? "34"
print("x: \(x)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment