Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexwal/bf408ae0759e77d0050e20d073dd8951 to your computer and use it in GitHub Desktop.
Save alexwal/bf408ae0759e77d0050e20d073dd8951 to your computer and use it in GitHub Desktop.
/// A fun Swift 5 way to concatenate a collection of String? elements, for example a [String?].
extension String.StringInterpolation {
mutating func appendInterpolation(_ array: [String?]) {
appendLiteral(array.compactMap { $0 }.joined(separator: ", "))
}
}
let array: [String?] = [nil, nil, "hello", nil, "world", nil, "abc", nil, nil, nil, nil]
print(array) // prints "[nil, nil, Optional("hello"), nil, Optional("world"), nil, Optional("abc"), nil, nil, nil, nil]"
print("\(array)") // prints "hello, world, abc"
@alexwal
Copy link
Author

alexwal commented Dec 2, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment