Last active
December 2, 2020 03:44
-
-
Save alexwal/bf408ae0759e77d0050e20d073dd8951 to your computer and use it in GitHub Desktop.
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
/// 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" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Even cooler stuff here https://www.hackingwithswift.com/articles/178/super-powered-string-interpolation-in-swift-5-0