Skip to content

Instantly share code, notes, and snippets.

@anzfactory
Created April 11, 2019 10:53
Show Gist options
  • Save anzfactory/9cb441895357e1d0d964d4d27bda0893 to your computer and use it in GitHub Desktop.
Save anzfactory/9cb441895357e1d0d964d4d27bda0893 to your computer and use it in GitHub Desktop.
Swift で Optional 型のやつを文字列に埋め込んだときの出力を変える
let text: String = "nullable"
let num: Int? = nil
print("text: \(text) num: \(num)")
/* output
> text: nullable num:
*/
extension String.StringInterpolation {
mutating func appendInterpolation(_ value: CustomStringConvertible?) {
guard let value = value else {
return
}
appendLiteral(String(describing: value))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment