How can I flatten an array swiftily in Swift?
问题 I want to turn this: let x = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] into this: [1, 2, 3, 4, 5, 6, 7, 8, 9] very gracefully. The most straightforward way, of course, is var y = [Int]() x.forEach { y.appendContentsOf($0) } But that makes the resulting array mutable, which is unnecessary. I don't like this way. I tried using reduce : let y = x.reduce([Int]()) { (array, ints) -> [Int] in array.appendContentsOf(ints) return array } But the compiler complains that array is immutable, so I can't call the