Skip to content

Instantly share code, notes, and snippets.

@natecook1000
Created June 2, 2023 01:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save natecook1000/66e641c961dc938157cfc7acb9b3d25e to your computer and use it in GitHub Desktop.
Save natecook1000/66e641c961dc938157cfc7acb9b3d25e to your computer and use it in GitHub Desktop.
extension String.StringInterpolation {
mutating func appendInterpolation<T>(_ value: T?, default: @autoclosure () -> String) {
self.appendLiteral(value.map(String.init(describing:)) ?? `default`())
}
}
let num = Int("21")
print("The number is: \(num, default: "<invalid>")")
// The number is: 21
let num2 = Int("asd")
print("The number is: \(num2, default: "<invalid>")")
// The number is: <invalid>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment