Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Closures in Swift Closure Expressions Closure Expression Shorthand

From shorthand to plain syntax

Hi,

I am having hard time finding the plain syntax of the following shorthand code :

let numbers = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
let result = numbers.flatMap({ $0.map( {$0 * 2} ) })
print(result)

Would really much appreciate your help :)

This is is how far I could go :

let numbers = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
let result = numbers.flatMap({ (element: [Int]) -> [Int] in
    for number in element {
        let individualResult = number * 2
    }
    return element
})
print(result)

1 Answer

Paolo Scamardella
Paolo Scamardella
24,828 Points

That is because you are returning element inside the flatMap method instead of the individualResult. You are not even using the individualResult at all. You need to add a mutable array before the for in loop, then append the result to the mutable array inside the for in loop, and then return the mutable array after the for in loop.